码迷,mamicode.com
首页 > Web开发 > 详细

网站搭建 (第05天) 分类和归档

时间:2019-04-21 20:29:01      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:def   djang   形式   mod   .lib   log   pytho   append   定义   

一、分类

  在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,文章的更新编辑以此链接为准。欢迎关注源站文章!

网站搭建 (第05天) 分类和归档

标签:def   djang   形式   mod   .lib   log   pytho   append   定义   

原文地址:https://www.cnblogs.com/djcoder/p/10746374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!