python Django 个人博客首页 单表多表查询渲染

核心该要

  • setting.py 设置 USE_TZ = False 过滤month不好用时改为False(Mysql才会出现)
  • 日期归档查询 内置TruncMonth模块
  • date_format的使用将时间格式化为想要的格式
  • extra 注入sql语句,用原生sql语句
  • boot删格系统的练习
  • 个人站点自页面路由url的优化 **kwargs

这里写图片描述


setting时区设定

# 时区设置到上海  
# 注意不能在建表之后 ,修改时区可能会在运行时报错
TIME_ZONE = 'Asia/Shanghai'


# TruncMonth模块 时间过滤month 不好用时候,改为False(Mysql才会出现)
USE_TZ = False

URL路由

# 正则 个人站点url  \w+: 不包含非字符串
re_path('^(?P<username>\w+)$', views.home_site),

'''每个用户站点不用单独配url只需录制处访问路径即可'''
# 个人站点各种分类  标签|随笔|时间  ?P<别名>
re_path('^(?P<username>\w+)/(?P<condition>tag|category|archive)/(?P<param>.*)/$', views.home_site),

视图views

  • 若可能有多个参数时用**kwargs
  • 内置年月归档模块from django.db.models.functions import TruncMonth
  • locals()传递本视图所有参数给html
def home_site(request, username, **kwargs):

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

    if not user:
        return render(request, 'not_found.html')

    # 查询站点当前对象
    blog = user.blog
    # 当前用户活当前站点所有文章
    article_list = models.Article.objects.filter(user=user)
    if kwargs:  # 判断是个人首页,还是分类子页面
        condition = kwargs.get('condition')  # 只会是tag|category|archive
        param = kwargs.get('param')
        if condition == 'category':
            article_list = article_list.filter(category__title=param)
        elif condition == 'tag':
            article_list = article_list.filter(tags__title=param)
        else:
            year, month = param.split('-')  # 日起格式 2018-2 所以用- 切割
            article_list = article_list.filter(create_time__year=year, create_time__month=month)

    # 查询分类 及 文章数: 先找到与当前blog 相等的 blog,再聚合文章数  pk 分组
    cate_list = models.Category.objects.filter(blog=blog).annotate(c=Count('article')).values_list('title', 'c')

    # 查询标签名 及 文章数
    tag_list = models.Tag.objects.filter(blog=blog).annotate(c=Count('article')).values_list('title', 'c')

    # 再以每个文章的发布时间分组
    time_list = models.Article.objects.filter(user=user).extra(select={"y_m_date": "date_format(create_time,'%%Y-%%m')"}).values('y_m_date').annotate(c=Count('nid')).values_list('y_m_date', 'c')

    # 内置方法
    from django.db.models.functions import TruncMonth  # 年月归档
    # ret = models.Article.objects.filter(user=user).annotate(month=TruncMonth('create_time')).values('month')

    return render(request, 'home_site.html', locals())

html

  • 熟练运用boot
  • 删格化系统
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .header {
            width: 100%;
            height: 60px;
            background-color: #2e6da4;
        }

        .header .title {
            font-size: 18px;
            font-weight: 100;
            line-height: 60px;
            color: white;
            margin-left: 50px;
        }

        .backend {
            float: right;
            color: white;
            text-decoration: none;
            font-size: 19px;
            margin-right: 30px;
        }

        .pub_info {
            color: gray;
            margin-top: 10px;
            float: right;
        }
    </style>
    <link rel="stylesheet" href="/static/blog/bs/css/bootstrap.css">
</head>
<body>

<div class="header">
    <div class="content">
        <p class="title">
            <span>{{ blog }}</span>
            <a href="" class="backend">管理</a>
        </p>
    </div>

</div>

<div class="container">
    <div class="row">
        <div class="col-md-3">
            <!--左侧  标签分类-->
            <div class="panel panel-warning">
                <div class="panel-heading">
                    <h3 class="panel-title">我的标签</h3>
                </div>
                <div class="panel-body">
                    {% for tag in tag_list %}
                        <p><a href="/{{ username }}/tag/{{ tag.0 }}">{{ tag.0 }}({{ tag.1 }})</a></p>
                    {% endfor %}

                </div>
            </div>

            <!--左侧  随笔分类-->
            <div class="panel panel-danger">
                <div class="panel-heading">
                    <h3 class="panel-title">随笔分类</h3>
                </div>
                <div class="panel-body">
                    {% for cate in cate_list %}
                        <p><a href="/{{ username }}/category/{{ cate.0 }}">{{ cate.0 }}({{ cate.1 }})</a></p>
                    {% endfor %}

                </div>
            </div>

            <!--左侧  时间归档-->
            <div class="panel panel-success">
                <div class="panel-heading">
                    <h3 class="panel-title">时间归档</h3>
                </div>
                <div class="panel-body">
                    {% for time in time_list %}
                        <p><a href="/{{ username }}/archive/{{ time.0 }}">{{ time.0 }}({{ time.1 }})</a></p>
                    {% endfor %}

                </div>
            </div>

        </div>

        <!--左侧  右侧分割线-->

        <div class="col-md-9">
            <div class="article_list">
                {% for article in article_list %}
                    <div class="article-item clearfix">  <!--清除多余浮动-->
                        <h5><a href="">{{ article.title }}</a></h5>
                        <div class="article-desc">
                            <span class="media-right">
                                {{ article.desc }}
                            </span>
                        </div>
                        <div class="small pub_info">  <!--样式设置小一点方便与摘要的区别-->
                            &nbsp;&nbsp;&nbsp;
                            <span>发布于&nbsp;&nbsp;{{ article.create_time|date:'Y-m-d  H:i' }}</span>
                            &nbsp;&nbsp;
                            <span class="glyphicon glyphicon-comment"></span>评论({{ article.comment_count }})
                            &nbsp;&nbsp;
                            <span class="glyphicon glyphicon-thumbs-up"></span>点赞({{ article.up_count }})
                            &nbsp;&nbsp;
                            <span class="glyphicon glyphicon-thumbs-down"></span>踩({{ article.down_count }})
                        </div>
                    </div>
                    <hr>
                {% endfor %}

            </div>
        </div>
    </div>
</div>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_42329277/article/details/82086388