• 【5】网站搭建:分类和归档


    一、分类

      在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>  {{ category }} ({% 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>  {% get_date_to_month post_date %} ({% 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 %}

      原文出处:https://jzfblog.com/detail/49,文章的更新编辑以此链接为准。欢迎关注源站文章!

    作者:蒋振飞
    此内容均来自个人原创以及互联网转载和摘录,仅供学习交流使用,并非商业用途,欢迎转载。
  • 相关阅读:
    dinoql 试用
    dinoql 使用graphql 语法查询javascript objects
    使用git_stats 统计分析git 仓库代码&& 集成webhook
    使用gitstats分析git 仓库代码
    PostGraphile 4.4 发布,支持real time 查询
    cube.js 学习(十)cube 来自官方的学习网站
    Optimize Cube.js Performance with Pre-Aggregations
    cube.js 学习(九)cube 的pre-aggregation
    cube.js 学习(八)backend部署模式
    cube.js 学习(七)cube.js type 以及format 说明
  • 原文地址:https://www.cnblogs.com/djcoder/p/10746374.html
Copyright © 2020-2023  润新知