Django学习:博客分类统计(14)
博客类别分类以及日期分类统计数量
- 修改
views.py
的get_blog_list_comment_data
函数
def get_blog_list_comment_data(request, blogs_all_list):
paginator = Paginator(blogs_all_list, settings.EACH_PAGE_BLOGS_NUMBER)
page_num = request.GET.get('page', 1) # 获取url的页面参数(GET请求)
page_of_blogs = paginator.get_page(page_num)
current_page_num = page_of_blogs.number # 获取当前页码
# 获取当前页前后各2页
page_range = list(range(max(current_page_num - 2, 1), current_page_num)) + list(range(current_page_num, min(current_page_num + 2, paginator.num_pages) + 1))
# 加上省略号
if page_range[0] - 1 >= 2:
page_range.insert(0, '...')
if paginator.num_pages - page_range[-1] >= 2:
page_range.append('...')
# 加上首尾页码
if page_range[0] != 1:
page_range.insert(0, 1)
if page_range[-1] != paginator.num_pages:
page_range.append(paginator.num_pages)
# 获取博客分类的对应博客数量
blog_types = BlogType.objects.all()
blog_types_list = []
for blog_type in blog_types:
# 为对象增加一个新的属性值blog_count
blog_type.blog_count = Blog.objects.filter(blog_type=blog_type).count()
blog_types_list.append(blog_type)
context = {}
context['blogs'] = page_of_blogs.object_list
context['page_range'] = page_range
context['page_of_blogs'] = page_of_blogs
context['blog_types'] = blog_types_list
context['blog_dates'] = Blog.objects.dates('created_time', 'month', order='DESC')
return context
- 修改
blog_list.html
增加对博客分类数量的引用
...snip....
<div class="panel-heading">博客分类</div>
<div class="panel-body">
<ul class='blog_types'>
{% for blog_type in blog_types %}
<li><a href='{% url "blogs_with_type" blog_type.pk %}'>{{ blog_type.type_name }}({{ blog_type.blog_count }})</a></li>
{% empty %}
<li>暂无分类</li>
{% endfor %}
</ul>
</div>
...snip...
刷新博客列表页
使用另外一种方法来实现上述的功能:annotate拓展查询字段
修改views.py
的get_blog_list_comment_data
函数
from django.db.models import Count
...snip...
context['blog_types'] = BlogType.objects.annotate(blog_count=Count('blog'))
...snip...
此时刷新页面可以得到同样的结果。
另外我们需要将日期归档也显示出具体的数量
- 修改
views.py
...snip...
def get_blog_list_comment_data(request, blogs_all_list):
paginator = Paginator(blogs_all_list, settings.EACH_PAGE_BLOGS_NUMBER)
page_num = request.GET.get('page', 1) # 获取url的页面参数(GET请求)
page_of_blogs = paginator.get_page(page_num)
current_page_num = page_of_blogs.number # 获取当前页码
# 获取当前页前后各2页
page_range = list(range(max(current_page_num - 2, 1), current_page_num)) + list(range(current_page_num, min(current_page_num + 2, paginator.num_pages) + 1))
# 加上省略号
if page_range[0] - 1 >= 2:
page_range.insert(0, '...')
if paginator.num_pages - page_range[-1] >= 2:
page_range.append('...')
# 加上首尾页码
if page_range[0] != 1:
page_range.insert(0, 1)
if page_range[-1] != paginator.num_pages:
page_range.append(paginator.num_pages)
# 获取博客分类的对应博客数量
'''blog_types = BlogType.objects.all()
blog_types_list = []
for blog_type in blog_types:
# 为对象增加一个新的属性值blog_count
blog_type.blog_count = Blog.objects.filter(blog_type=blog_type).count()
blog_types_list.append(blog_type)
'''
# 获取日期归档的对应博客数量
blog_dates = Blog.objects.dates('created_time', 'month', order='DESC')
blog_dates_dict = {}
for blog_date in blog_dates:
blog_count = Blog.objects.filter(created_time__year=blog_date.year,
created_time__month=blog_date.month).count()
blog_dates_dict[blog_date] = blog_count
...snip...
- 修改
blog_list.html
...snip...
<div class="panel panel-default">
<div class="panel-heading">日期归档</div>
<div class="panel-body">
<ul>
{% for blog_date, blog_count in blog_dates.items %}
<li>
<a href="{% url 'blogs_with_date' blog_date.year blog_date.month %}">{{ blog_date|date:"Y年m月" }}({{ blog_count }})</a>
</li>
{% endfor %}
</ul>
</div>
</div>
...snip...
刷新页面显示如下: