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

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

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

    在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 %}
相关文章
|
5月前
|
消息中间件 Java Linux
RocketMQ的下载与安装(全网最细保姆级别教学)
RocketMQ的下载与安装(全网最细保姆级别教学)
1978 1
|
5月前
|
存储 弹性计算 Windows
雾锁王国如何实现存档迁移?
如何把本地存档数据迁移到服务器?如何把一个服务器数据迁移到另一个服务器?如何把Steam云存档数据迁移到本地?
2498 0
|
3天前
|
SQL 存储 人工智能
阿里云日志服务的傻瓜式极易预测模型
预测服务有助于提前规划,减少资源消耗和成本。阿里云日志服务的AI预测服务简化了数学建模,仅需SQL操作即可预测未来指标,具备高准确性,并能处理远期预测。此外,通过ScheduledSQL功能,可将预测任务自动化,定时执行并保存结果。
21 3
|
5月前
|
数据安全/隐私保护
雾锁王国搭建、存档、使用常见问题汇总(持续更新)
使用雾锁王国的常见问题,例如怎么搭建私服、搜索不到服务器怎么办、存档数据怎么迁移.......
3165 1
雾锁王国搭建、存档、使用常见问题汇总(持续更新)
|
5月前
|
网络协议 安全 Go
子域名收集神器:Subfinder 保姆级教程(附链接)
子域名收集神器:Subfinder 保姆级教程(附链接)
|
5月前
|
Linux 网络安全 数据安全/隐私保护
幻兽帕鲁服务器部署保姆级教程(持续更新)
幻兽帕鲁服务器部署保姆级教程(持续更新)
274 1
|
5月前
|
消息中间件 缓存 监控
阿里P8整理的《百亿级并发系统设计》实战教程,实在是太香了
说实话,如果面试官问你这个题目,那么你必须要使出全身吃奶劲了。为啥?因为你没看到现在很多公司招聘的 JD 里都是说啥有高并发经验者优先。
|
存储 安全 程序员
个人关于阿里云存储使用的心得体会
众所周知,阿里云存储是国内领先的云存储服务提供商,其提供的存储服务包括对象存储(OSS)、文件存储(NAS)等多种形式,可以满足不同场景下的存储需求。我作为一名程序员,个人在工作中经常使用阿里云存储服务,下面分享一下个人关于阿里云存储服务使用的心得体会。
490 1
个人关于阿里云存储使用的心得体会
|
小程序 安全 数据库
手把手教你搭建消防安全答题小程序-在结果页中实现从云数据库查询成绩
手把手教你搭建消防安全答题小程序-在结果页中实现从云数据库查询成绩
手把手教你搭建消防安全答题小程序-在结果页中实现从云数据库查询成绩
|
弹性计算 Linux 网络安全
【在家实践】准服务端开发ECS学习使用体验记录
笔者是机械研三的学生,自己在研究生期间自学后,在秋招拿到服务端开发的职位,确定工作之后想在就职前先预习下各种技术(没错我就是卷王,我只想卷死各位,或者被各位卷死(狗头))。其实在之前自学的时候已经接触过Linux系统,不过一直是在虚拟机里使用,而因为我从事的是游戏开发,又想用untiy做些客户端的工作开发,这就导致同时开虚拟机和unity时我的小笔电有些吃不消。所以准备把服务端的运行放在云上。几番比较以后,了解到阿里云的技术以及服务比相同产品优势大,所以果断先试用了一个月,最近放假在家一个月到期,但想继续学习使用,发现了【在家实践】这个学生(白嫖)活动,赶紧参与一手!!
【在家实践】准服务端开发ECS学习使用体验记录