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

查询个人站点的文章、分类和标签查询

时间:2019-02-17 10:54:46      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:ext   not   UNC   名称   style   cat   inf   mod   filter   

urls.py

re_path(‘^(?P<username>\w+)$‘, views.home_site, name=‘home_site‘),

 

home_site.py

def home_site(request, username):
    """
    个人站点视图函数
    :param request:
    :return:
    """

    user = UserInfo.objects.filter(username=username).first()

    # 判断用户是否存在
    if not user:
        return render(request, ‘not_found.html‘)

    # 查询当前站点
    blog = user.blog

    # 获取当前用户或者当前站点对应的所有文章
    # 基于对象查询
    # aritlce_list = user.article_set.all()

    # 基于双下划线查询
    article_list = models.Article.objects.filter(user=user)

    # 查询当前站点的每一个分类名称以及对应的文章数
    category_list = models.Category.objects.filter(blog=blog).values(‘pk‘).annotate(
        count=Count(‘article__title‘)).values(
        ‘title‘, ‘count‘)

    # 查询当前站点的每一个标签名称以及对应的文章数
    tag_list = models.Tag.objects.filter(blog=blog).values(‘pk‘).annotate(count=Count(‘article‘)).values_list(
        ‘title‘, ‘count‘
    )

    # 查询当前站点的每一个年月名称以及对应的文章数

    date_list = models.Article.objects.filter(user=user).annotate(month=TruncMonth(‘created_time‘)).values(
        ‘month‘).annotate(
        count=Count(‘nid‘)).values_list(
        ‘month‘, ‘count‘)
    # 其他复杂的没有这种方法的还是要用extras这个接口自己写

    return render(request, ‘home_site.html‘)

 

查询个人站点的文章、分类和标签查询

标签:ext   not   UNC   名称   style   cat   inf   mod   filter   

原文地址:https://www.cnblogs.com/lshedward/p/10390199.html

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