• BBS项目之个人站点


    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>
    
  • 相关阅读:
    VS2019 开发CMake项目
    node实验1
    DX9动画加载
    Excel表格的自动化处理和推送(二)信息分发
    案例分享:Excel表格的自动化处理和推送
    快速信息分发应用分发工资条明细
    在EasySQLMAIL中实现表格的行列转置
    从命令行启动EasySQLMAIL中的信息推送任务
    version `GLIBCXX_3.4.20' not found 解决方法
    【面试系列】如何保障质量之测试左移右移
  • 原文地址:https://www.cnblogs.com/surpass123/p/13149845.html
Copyright © 2020-2023  润新知