我的网站搭建: (第五天) 分类和归档

 文章分类日期归档能帮助我们快速定位到想要查找内容,所以今天就是要实现分类以及归档,先从分类开始。

    在blog/views.py中定义一个博客分类的视图函数:

def category_list(request):
    """
        作用:博客分类的视图处理
        request:请求对象
    """
    # 获得所有的分类
    category_list = Category.objects.all()

    context = {'category_list': category_list}
    return render(request, 'blog/category_list.html', context)

    通过该视图函数,可以发现其向模板发送了category_list,也就是所有分类的列表,但是并没有对该分类下的文章进行数量计算,有两种方法可以通过category对象得到其对应的文章数量

    方法一:

# 获取博客分类对应的博客数量
# 通过对每个category绑定一个post_count属性
category_list = []
    for category in category_list:
        category.post_count = Post.objects.filter(category=category).count()
        category_list.append(category)

    方法二:使用模板标签,简单使用方法如:Django入门: (第十二天) 自定义过滤器和标签   

        在templatetags文件夹中创建一个post_tags.py文件

from django import template
from blog.models import Post

register = template.Library()


@register.simple_tag
def get_category_post(category):
    """
        作用:获取该目录下的所有博客
        obj:模板传递的参数,也就是category
    """
    post_list = Post.objects.filter(category=category)
    return post_list[0:15]


@register.simple_tag
def get_category_count(category):
    """
        作用:计算当前分类的文章数量,并返回到模板中
        category:模板页面传入的category
    """
    return Post.objects.filter(category=category).count()

        前端代码实现:

{% load post_tags %}
...
{% for category in category_list %}
    {% get_category_post category as post_list %}
    <div class="col-sm-4">
        <div class="category">

            <h4>
                <a href="{% url 'blog:category' category.pk %}">
                    <span class="glyphicon glyphicon-file"></span>&nbsp;&nbsp;{{ category }}&nbsp;({% get_category_count category %})
                </a>
            </h4>

            <div>
                {% for post in post_list %}
                    <div class="light-blog"><a href="{% url 'blog:detail' post.id %}">{{ post }}</a></div>
                {% empty %}
                    <h5>暂无博客</h5>
                {% endfor %}
            </div>

        </div>
    </div>
{% empty %}
    <h3>暂无分类!</h3>
{% endfor %}

    在blog/views.py中定义一个日期归档的视图函数:

def date_list(request):
    """
        作用:日期归档的视图处理
        request:请求对象
    """
    date_list = Post.objects.dates('created_time', 'month', order='DESC')
    post_count = Post.objects.all().count()

    context = {'date_list': date_list,
               'post_count': post_count,}
    return render(request, 'blog/date_list.html', context)

    同样的,日期归档也与上述非常相似,但是不能采用类似于方法一的解决方案,因为并没有设计关于日期的数据表,无法给日期直接绑定一个字段名,可是其也有两种方法,即使用字典模板标签

    方法一:

# 获取日期归档对应的博客数量
# 利用字典
post_date_dict = {}
for post_date in date_list:
    post_date_count = Post.objects.filter(created_time__year=post_date.year, created_time__month=post_date.month).count()
    post_date_dict[post_date] = post_date_count

    方法二:加入模板标签,在post_tags.py文件添上

@register.simple_tag
def get_date_post(year, month):
    """
        作用:获取该年月下的博客
        year:模板传递的年份
        month:模板传递的月份
    """
    post_list = Post.objects.all().filter(created_time__year=year, created_time__month=month)
    return post_list[:15]

@register.simple_tag
def get_date_to_month(post_date):
    """
        作用:将日期格式转换成年月的形式
        obj: 对应的post_date
    """
    return (str(post_date.year) +'年' + str(post_date.month) + '月')

@register.simple_tag
def get_date_count(year, month):
    """
        作用:获得该年月下的博客数量
        year: 模板传递的年份
        month:模板传递的月份 
    """
    return Post.objects.filter(created_time__year=year, created_time__month=month).count()

    前端代码实现:

{% for post_date in date_list %}
    {% get_date_post post_date.year post_date.month as post_list %}

    <div class="col-xs-12 col-sm-4">
        <div class="category">
            <h4>
                <a href="{% url 'blog:date' post_date.year post_date.month %}" style="color: #333; font-weight: bold" >
                    <span class="glyphicon glyphicon-book"></span>&nbsp;&nbsp;{% get_date_to_month post_date %}&nbsp;({% get_date_count post_date.year post_date.month %})
                </a>
            </h4>

            <div>
                {% for post in post_list %}
                    <div class="light-blog">
                        <a href="{% url 'blog:detail' post.id %}">{{ post }}</a>
                    </div>
                {% empty %}
                    <h5>暂无博客</h5>
                {% endfor %}
        </div>
    </div>
</div>
{% empty %}
    <h3>暂无分类!</h3>
{% endfor %}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值