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

简介:  文章分类和日期归档能帮助我们快速定位到想要查找内容,所以今天就是要实现分类以及归档,先从分类开始。    在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 %}
相关文章
|
存储 域名解析 网络协议
windows server安装域控及原理(小白向)
windows server安装域控及原理(小白向)
704 0
|
分布式计算 DataWorks API
一分钟“零代码”生成API,DataWorks数据服务快速上手指南
DataWorks数据服务提供了快速生成API的能力,可以一分钟“零代码”生成API,本文是一篇快速上手指南,帮助你快速认识数据服务。
12320 0
|
存储 数据可视化 API
1688商品详情数据接口:如何通过1688 API实现批量商品数据抓取和分析
使用1688 API进行批量商品数据抓取和分析,首先需注册账号创建应用获取App Key和Secret Key。研究API文档,构建请求URL,如商品详情、搜索、销售量等接口。利用编程语言发送HTTP请求,实时抓取并处理数据,存储到数据库。实施优化策略,处理错误,记录日志。数据可视化展示并确保API安全性。编写文档并持续更新以适应API变化。参考[c0b.cc/R4rbK2]获取API测试和SDK。
|
Prometheus 监控 Kubernetes
prometheus学习笔记之简介与安装
prometheus学习笔记之简介与安装
prometheus学习笔记之简介与安装
|
关系型数据库 MySQL
蓝易云 - MySQL自动删除binlog日志
注意,这个参数只影响新的binlog文件。如果你的服务器上已经有超过7天的日志文件,你需要手动删除它们,或者使用PURGE BINARY LOGS命令来删除它们。
130 0
|
测试技术 Python
【手机群控】 利用Python与uiautomator2实现
使用Python的uiautomator2库进行多设备自动化测试,涉及环境准备(Python、uiautomator2、adb连接设备)和代码实现。通过`adb devices`获取设备列表,使用多进程并行执行测试脚本,每个脚本通过uiautomator2连接设备并获取屏幕尺寸。注意设备需开启USB调试并授权adb。利用多进程而非多线程,因Python的GIL限制。文章提供了一种提高测试效率的方法,适用于大规模设备测试场景。
963 2
【手机群控】 利用Python与uiautomator2实现
|
11月前
|
Web App开发 缓存 安全
Chrome浏览器启动参数大全
这是一组用于定制浏览器行为的命令行参数,包括但不限于:不停用过期插件、放行非安全内容、允许应用中心脚本、停用GPU加速视频、禁用桌面通知、禁用拓展及各类API、调整缓存设置、启用打印预览、隐身模式启动、设定语言、使用代理服务器、无头模式运行等。通过这些参数,用户可以根据需求灵活调整浏览器功能与性能。
|
弹性计算 小程序 关系型数据库
24.8|超值超省,开发者轻创业算力补贴申请教程
阿里云推出一系列优惠活动,助力企业和开发者高效上云。核心优惠包括:“99计划”提供超低价服务器资源,老用户也可享受;5亿算力补贴与满减优惠降低上云成本;免费试用覆盖多种产品;创业者计划提供高额抵扣金;多款产品组合满足不同需求。学生更可专享300元补贴。此外,还介绍了开发者如何利用低成本云资源通过广告变现,包括不同广告计费模式及成功案例分析。这些优惠和服务有助于企业快速成长与数字化转型。
224 2
24.8|超值超省,开发者轻创业算力补贴申请教程
|
安全 Java C++
CAS自旋锁到底是什么?为什么能实现线程安全?
本文是博主对多线程学习总结记录,希望对大家有所帮助。
1444 0
CAS自旋锁到底是什么?为什么能实现线程安全?
|
机器学习/深度学习 人工智能 资源调度
GPU计算资源智能调度:过去、现在和未来
随着AI和大数据技术发展,GPU成为关键计算组件。文章探讨了GPU计算资源调度从静态到动态再到智能调度的演变,现以机器学习优化资源利用率。未来趋势包括自适应调度、跨平台、集群级调度和能源效率优化,旨在提升GPU性能,推动人工智能和大数据领域进步。

热门文章

最新文章