zoukankan      html  css  js  c++  java
  • 查询个人站点的文章、分类和标签查询

    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')

  • 相关阅读:
    新的开始!
    find命令之mtime
    glances服务器监控工具
    centos7最小化安装改为图形界面
    ansible笔记(3)-文件操作模块(上)
    php-fpm参数详解
    ansible笔记(2)-模块简介
    centos创建交换分区
    等保测评三级整改-身份鉴别
    vsftp安装配置
  • 原文地址:https://www.cnblogs.com/lshedward/p/10390199.html
Copyright © 2011-2022 走看看