码迷,mamicode.com
首页 > 其他好文 > 详细

BBS项目之个人站点

时间:2020-06-17 01:18:35      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:UNC   截取   一个   split   显示   相关   页面布局   ons   filter   

BBS项目之个人站点

个人站点的需求分析

筛选出该站点下的所有文章,并分组展示,按以下几点进行分组:

(1)按文章分类分组:
(2)按文章标签分组;
(3)按年月分组;

个人站点采用col-md-2 col-md-10的页面布局,左侧采用面板显示分组条件,右侧显示筛选出的结果。

图 1。

  • 文章按分类分组
# 查询当前站点下所有分类以及分类的文章数
blog = user_obj.blog
category_list = models.Category.objects.filter(blog=blog).annotate(count_num=Count(‘article__pk‘)).values_list(‘name‘, ‘count_num‘, ‘pk‘)

# 分组查询
"""
<QuerySet [(‘surpass分类(一)‘, 3), (‘surpass分类(二)‘, 2), (‘surpass分类(三)‘, 2)]>
"""
  • 文章按标签进行分组
# 查询当前站点下的所有标签以及标签下的文章数
tag_list = models.Tag.objects.filter(blog=blog).annotate(count_num=Count(‘article__pk‘)).values_list(‘name‘,‘count_num‘,‘pk‘)

"""
<QuerySet [(‘surpass标签(一)‘, 3), (‘surpass标签(二)‘, 4)]>
"""
  • 文章按年月分组
# 日期归档查询使用django提供的TruncMonth自动按月截取,形成一个虚拟字段用于日期分组

from django.db.models.functions import TruncMonth

 # 按照年月统计当前站点所有的文章
    date_list = models.Article.objects.filter(blog=blog).annotate(month=TruncMonth(‘create_time‘)).values(‘month‘).annotate(count_num=Count(‘pk‘)).values_list(‘month‘, ‘count_num‘)

这里需要注意的是:

annotate在values前面,查询按annotate的分组字段查询;
annotate在values后面,查询按照values的字段查询。

点击文章标签分组、文章分类分组、日期归档分类时如何设计跳转和过滤相关文章信息。

  • 个人站点首页的文章是该用户的全部文章,而标签分组这些都是附加一些其他过滤条件的文章。
  • 可以设计一条新的url,处理该url的视图函数进一步过滤符合条件的文章。
  • 进一步过滤时是在当前站点用户文章的基础上进一步附加附属限制条件过滤的。
  • 因此,可以考虑处理新url的视图函数和个人站点的的url的公用一个视图函数。
  • 不同的是个人站点的url需要要一个用户名就可以唯一标识;而新url不仅需要用户名还需要两个参数:标签|分类|归档中的一种情况,另一个参数唯一表示该情况下的具体值。
 # 个人站点页面的搭建
    url(r‘^(?P<username>\w+)/$‘, views.site, name=‘site‘),
 # 侧边栏筛选功能
    url(r‘^(?P<username>\w+)/(?P<condition>category|tag|archive)/(?P<param>.*)/‘, views.site),

个人站点后端代码:

def site(request, username, **kwargs):
    user_obj = models.UserInfo.objects.filter(username=username).first()
    blog = user_obj.blog
    # 如果用户不存在返回一个404页面
    if not user_obj:
        return render(request, ‘error.html‘)
    # 查询当前用户个人站点下的所有文章
    blog = user_obj.blog
    article_list = models.Article.objects.filter(blog=blog).all()
    # 侧边栏的筛选
    if kwargs:
        condition = kwargs.get(‘condition‘)
        param = kwargs.get(‘param‘)
        if condition == ‘category‘:
            article_list = article_list.filter(category_id=param)
        elif condition == ‘tag‘:
            # 多对多跨表查询
            article_list = article_list.filter(tags__id=param)
        else:
            year, month = param.split(‘-‘)
            article_list = article_list.filter(create_time__year=year, create_time__month=month)
    current_page = request.GET.get("page", 1)
    record_count = article_list.count()
    page_obj = pager.Pager(current_page, record_count, per_page_num=3, pager_count=11)
    page_queryset = article_list[page_obj.start:page_obj.end]

query_set对象可以进一步进行filtter

前端页面left-menu.html

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">文章分类</h3>
    </div>
    <div class="panel-body">
        {% for category in category_list %}
            <div>
                <a href="/{{ username }}/category/{{ category.2 }}"
                   style="text-decoration: none">{{ category.0 }}({{ category.1 }})</a>
            </div>
        {% endfor %}
    </div>
</div>
<div class="panel panel-danger">
    <div class="panel-heading">
        <h3 class="panel-title">文章标签</h3>
    </div>
    <div class="panel-body">
        {% for tag in tag_list %}
            <div>
                <a href="/{{ username }}/tag/{{ tag.2 }}"
                   style="text-decoration: none">{{ tag.0 }}({{ tag.1 }})</a>
            </div>
        {% endfor %}
    </div>
</div>
<div class="panel panel-info">
    <div class="panel-heading">
        <h3 class="panel-title">日期归档</h3>
    </div>
    <div class="panel-body">
        {% for date in date_list %}
            <div>
                <a href="/{{ username }}/archive/{{ date.0|date:‘Y-m‘ }}"
                   style="text-decoration: none">{{ date.0|date:‘Y年m月‘ }}({{ date.1 }})</a>
            </div>
        {% endfor %}
    </div>
</div>

BBS项目之个人站点

标签:UNC   截取   一个   split   显示   相关   页面布局   ons   filter   

原文地址:https://www.cnblogs.com/surpass123/p/13149845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!