技术交流QQ群:1027579432,欢迎你的加入!
本教程来源于B站杨仕航Django2.0开发视频教程,如需转载,必须注明来源!
1.按博客类型分类统计博客数量
- 方法1:给BlogType模型手动增加一个blog_count字段信息,便于统计属于某种类型下的博客数量。打开blog文件目录下的views.py文件,修改blog_list_common_data()增加获取某一特定类别的博客数量功能的代码段,如下所示:
def blog_list_common_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 # 获取当前页码 # 获取当前页码前后各两页的页码范围 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) # 方法1:获取某一特定类别的博客数量 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['blogs'] = page_of_blogs.object_list context['page_of_blogs'] = page_of_blogs context['page_range'] = page_range context['blog_types'] = blog_types_list context['blog_dates'] = Blog.objects.dates( "created_time", "month", order="DESC") return context
- 接着,修改templates文件夹下的blog_list.html模板文件,增加显示属于某一特定类别的博客数量代码。如下所示:
<div class="panel panel-default"> <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> </div>
- 方法2:annotate注释:使用annotate扩展查询字段。
from django.shortcuts import render_to_response, get_object_or_404 from django.core.paginator import Paginator from django.conf import settings from django.db.models import Count from .models import Blog, BlogType # Create your views here. def blog_list_common_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 # 获取当前页码 # 获取当前页码前后各两页的页码范围 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) # 方法2:获取某一特定类别的博客数量 # BlogType.objects.annotate(blog_count=Count("blog")) context = {} context['blogs'] = page_of_blogs.object_list context['page_of_blogs'] = page_of_blogs context['page_range'] = page_range context['blog_types'] = BlogType.objects.annotate(blog_count=Count("blog")) context['blog_dates'] = Blog.objects.dates( "created_time", "month", order="DESC") return context
2.按日期归档统计博客数量
- 打开blog文件目录下的views.py文件,修改blog_list_common_data()增加获取按日期归档统计博客数量功能的代码段,如下所示:
def blog_list_common_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 # 获取当前页码 # 获取当前页码前后各两页的页码范围 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) # 方法1:获取某一特定类别的博客数量 ''' 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) ''' # 方法2:获取某一特定类别的博客数量 # BlogType.objects.annotate(blog_count=Count("blog")) # 方法:获取按日期归档对应的博客数量 # blog_dates是一个datetime数据类型,即<QuerySet [datetime.date(2020, 5, 1)]> 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['blogs'] = page_of_blogs.object_list context['page_of_blogs'] = page_of_blogs context['page_range'] = page_range context['blog_types'] = BlogType.objects.annotate(blog_count=Count("blog")) context['blog_dates'] = blog_dates_dict return context
- 接着,修改templates文件夹下的blog_list.html模板文件,增加显示按日期归档显示博客数量的代码。如下所示:
<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>
网友评论