Django2.0:【Django2.0教程】16.博客分类统计 视频学习笔记

视频链接: 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值