【一】统计分类、标签下的文章数目
【1】模板标签导入
from django.db.models import Count
【2】后端( 对文章分类标签进行归档分类)
@register.inclusion_tag("inclusion_tags/article_left.html")
def article_left(request):
# 【一】携带参数进来 : 当前点击的用户对象的用户名
path = request.get_full_path()
#切割出用户名
username = path.split("/")[1]
# 【二】查询得到当前用户的用户对象
user_obj = UserInfo.objects.get(username=username)
# 获取到当前用户下的所有文章和文章分类
article_category_class_data = CategoryClass.objects.filter(blog=user_obj.blog).annotate(
# 统计当前用户下各个分类的文章数
article_num=Count("article__pk")
).values("id", "name", "article_num")
# 获取当前用户下的所有标签和标签下的文章数
article_category_tag_data = CategoryTag.objects.filter(blog=user_obj.blog).annotate(
article_num=Count("article__pk")
).values("id", "article_num", "name")
# 获取当前用户下的所有文章并且对文章进行归档和分类
article_year_month_data = Article.objects.filter(blog=user_obj.blog).annotate(
month=TruncMonth("create_time")).annotate(
article_num=Count("pk")
).values("month", "article_num")
# 获取到当前文章点赞数靠前的文章
article_recommend_data = Article.objects.filter(blog=user_obj.blog).order_by('-up_num').values("title", "up_num")
return locals()
【3】settings.py
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
【4】前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">我的标签</h3>
</div>
<div class="panel-body">
{% for article_category_class_obj in article_category_class_data %}
<div><a href="">{{ article_category_class_obj.name }} ({{ article_category_class_obj.article_num }})</a>
</div>
<hr>
{% endfor %}
</div>
</div>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">随笔分类</h3>
</div>
<div class="panel-body">
{% for article_category_tag_obj in article_category_tag_data %}
<div><a href="">{{ article_category_tag_obj.name }} ({{ article_category_tag_obj.article_num }})</a></div>
<hr>
{% endfor %}
</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">随笔档案</h3>
</div>
<div class="panel-body">
{% for article_year_month_obj in article_year_month_data %}
<div><a href="">{{ article_year_month_obj.month|date:"Y-m" }} ({{ article_year_month_obj.article_num }})</a>
</div>
<hr>
{% endfor %}
</div>
</div>
<div class="panel panel-warning">
<div class="panel-heading">
<h3 class="panel-title">推荐排行</h3>
</div>
<div class="panel-body">
{% for article_recommend_obj in article_recommend_data %}
<div><a href="">{{ article_recommend_obj.title }} ({{ article_recommend_obj.up_num }})</a></div>
<hr>
{% endfor %}
</div>
</div>
</body>
</html>