视频链接:
16.博客分类统计
为博客分类添加显示博客数量,效果如下图所示。这儿会使用两种方法实现。
为博客分类添加显示博客数量,效果如下图所示。这儿会使用两种方法实现。
修改blog/views.py:
...
def get_blog_list_common_data(request, blogs_all_list):
paginator = Paginator(blogs_all_list, settings.BLOGS_NUM_PER_PAGE)
page_num = request.GET.get('page', 1) # 获取url的页码参数。GET返回字典,page_num默认为1
page_of_blogs = paginator.get_page(page_num)
current_page_num = page_of_blogs.number # 获取当前页码
# 获取前后各页
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_type.blog_count = Blog.objects.filter(blog_type=blog_type).count()
blog_types_list.append(blog_type)
context = {}
context['page_of_blogs'] = page_of_blogs
# context['blogs_count'] = Blog.objects.all().count()
context['blog_types'] = blog_types_list
context['page_range'] = page_range
context['blog_dates'] = Blog.objects.dates('created_time', 'month', order='DESC') # created_time按月降序
return context
...
修改blog/templates/blog/blog_list.html:
<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>
annotate注释
修改blog/views.py:
...
from django.db.models import Count
...
def get_blog_list_common_data(request, blogs_all_list):
paginator = Paginator(blogs_all_list, settings.BLOGS_NUM_PER_PAGE)
page_num = request.GET.get('page', 1) # 获取url的页码参数。GET返回字典,page_num默认为1
page_of_blogs = paginator.get_page(page_num)
current_page_num = page_of_blogs.number # 获取当前页码
# 获取前后各页
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)
context = {}
context['page_of_blogs'] = page_of_blogs
# context['blogs_count'] = Blog.objects.all().count()
context['blog_types'] = BlogType.objects.annotate(blog_count=Count('blog'))
context['page_range'] = page_range
context['blog_dates'] = Blog.objects.dates('created_time', 'month', order='DESC') # created_time按月降序
return context
...
继续为日期归档添加博客数量。效果下图所示。
修改blog/views.py:
...
def get_blog_list_common_data(request, blogs_all_list):
paginator = Paginator(blogs_all_list, settings.BLOGS_NUM_PER_PAGE)
page_num = request.GET.get('page', 1) # 获取url的页码参数。GET返回字典,page_num默认为1
page_of_blogs = paginator.get_page(page_num)
current_page_num = page_of_blogs.number # 获取当前页码
# 获取前后各页
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_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
context = {}
context['page_of_blogs'] = page_of_blogs
# context['blogs_count'] = Blog.objects.all().count()
context['blog_types'] = BlogType.objects.annotate(blog_count=Count('blog'))
context['page_range'] = page_range
context['blog_dates'] = blog_dates_dict
return context
...
修改blog/templates/blog/blog_list.html:
<div class="panel-heading">日期归档</div>
<div class="panel-body">
<ul class="blog-types">
{% 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>