您的位置:首页 > 编程语言 > Go语言

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

2017-10-22 17:33 363 查看

编写模板文件

首先编写
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 hr
a857
ef="#">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>


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

本文相关源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: