编写模板文件
首先编写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),方便快速跳转
由于tag
和post
是多对一关系,通过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
的每一项拥有两个属性grouper
和list
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>
这样标签、分类和归档页面就完成,通过点击导航栏就可以进入对应页面了