Django 博客 - 6 标签、分类和归档页面

编写模板文件

首先编写tag的模板,新建blog/templates/blog/tags.html文件,假设传给模版的上下文里有一个tags,代表所有标签,因此模板可以这么写

<ul>
    {% for tag in tags %}
    <li>
        <h3 id="{{ tag.name }}">{{ tag.name }}</h3>
        <ul>
            {% for post in tag.post_set.all %}
            <li><a href="{{ post.get_absolute_url }}">{{post.title }}</a></li>
            {% endfor %}
        </ul>
    </li>
    {% endfor %}
</ul>

这里将id也设置为tag.name,主要是设置锚(archors),方便快速跳转
由于tagpost是多对一关系,通过tag.post_set.all,可以获取tag对应的所有文章

再使用UIKit3美化一下

{% extends 'blog/base.html' %}
{% block title %}Tags{% endblock %}
{% block body %}
    <div class="uk-container">
        <ul uk-accordion="multiple: true">
            {% for tag in tags %}
                <li class="uk-open">
                    <h3 class="uk-accordion-title" id="{{ tag.name }}">{{ tag.name }}</h3>
                    <div class="uk-accordion-content">
                        <ul>
                            {% for post in tag.post_set.all %}
                                <li><a href="{{ post.get_absolute_url }}" class="uk-link-reset uk-button-text">{{ post.title }}</a></li>
                            {% endfor %}
                        </ul>
                    </div>
                </li>
            {% endfor %}
        </ul>
    </div>
{% endblock %}

同样的,category的模板和tag类似
新建blog/templates/blog/category.html,内容如下

{% extends 'blog/base.html' %}
{% block title %}My Blog{% endblock %}
{% block body %}
    <div class="uk-container">
        <ul uk-accordion="multiple: true">
            {% for category in categories %}
                <li class="uk-open">
                    <h3 class="uk-accordion-title" id="{{ category.name }}">{{ category.name }}</h3>
                    <div class="uk-accordion-content">
                        <ul>
                            {% for post in category.post_set.all %}
                                <li><a href="{{ post.get_absolute_url }}" class="uk-link-reset uk-button-text">{{ post.title }}</a></li>
                            {% endfor %}
                        </ul>
                    </div>
                </li>
            {% endfor %}
        </ul>
    </div>
{% endblock %}

再来编写archive页面,归档页面需要按年再按月分类,但是我们模板里只有博客列表posts,需要将博客分组,模板引擎提供了regroup标签,可以方便的对已排序的列表,再重新按要求分组

如果将posts按年分组,只需像下面这么写
{% regroup posts by created_time.year as posts_by_year %}
最终得到的分组列表posts_by_year的每一项拥有两个属性grouperlist
grouper是分组的名字,这里就是年份了,list是当前分组的列表,这里就是同一年的博客,因此可以继续按月分组
{% regroup year.list by created_time.month as posts_by_month %}

regroup的用法可以查看官方文档

继续来创建模板,新建blog/templates/blog/archive.html

{% extends 'blog/base.html' %}
{% block title %}My Blog{% endblock %}
{% block body %}
<div class="uk-container">
{% regroup posts by created_time.year as posts_by_year %}
<ul>
    {% for year in posts_by_year %}
        <li>{{ year.grouper }}{% regroup year.list by created_time.month as posts_by_month %}
        <ul>
            {% for month in posts_by_month %}
                <li id="{{ year.grouper }}{{ month.grouper|stringformat:'02d' }}">{{ month.grouper }}<ul>
                        {% for post in month.list %}
                            <li>{{ post.created_time.day }}<a href="{{ post.get_absolute_url }}" class="uk-link-reset uk-button-text">{{ post.title }}</a> </li>
                        {% endfor %}
                    </ul>
                </li>
            {% endfor %}
        </ul>
        </li>
    {% endfor %}
</ul>
</div>
{% endblock %}

编写视图

blog/views.py添加

class TagView(ListView):
    model = Tag
    context_object_name = 'tags'
    template_name = 'blog/tags.html'


class CategoryView(ListView):
    model = Category
    context_object_name = 'categories'
    template_name = 'blog/category.html'


class ArchiveView(ListView):
    model = Post
    context_object_name = 'posts'
    template_name = 'blog/archive.html'

和主页一样,使用ListView,只需要指定几个属性

配置Url

blog/urls.py添加

urlpatterns = [
    ...
    url(r'^tag/$', views.TagView.as_view(), name='tag'),
    url(r'^category/$', views.CategoryView.as_view(), name='category'),
    url(r'^archive/$', views.ArchiveView.as_view(), name='archive'),
]

修改blog/templates/blog/base.html,将对应的url通过url标签填入

             <div class="uk-navbar-left">
                 <ul class="uk-navbar-nav">
                     <li><a href="{% url 'blog:index' %}">Blog</a></li>
-                    <li><a href="#">Tag</a></li>
-                    <li><a href="#">Category</a></li>
-                    <li><a href="#">Achieve</a></li>
+                    <li><a href="{% url 'blog:tag' %}">Tag</a></li>
+                    <li><a href="{% url 'blog:category' %}">Category</a></li>
+                    <li><a href="{% url 'blog:archive' %}">Achieve</a></li>
                 </ul>
             </div>
         </nav>

这样标签、分类和归档页面就完成,通过点击导航栏就可以进入对应页面了

本文相关源码

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值