Django 归档, AboutMe和标签分类
归档
归档就是列出当前博客中所有的文章, 并且能够显示时间, 很容易的可以写出对应的view和模板来
在my_blog/article/views.py添加如下内容:
1 2 3 4 5 6 7
| def archives(request) : try: post_list = Article.objects.all() except Article.DoesNotExist : raise Http404 return render(request, 'archives.html', {'post_list' : post_list, 'error' : False})
|
在my_blog/templates新建模板archives.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| {% extends "base.html" %}
{% block content %} <div class="posts"> {% for post in post_list %} <section class="post"> <header class="post-header"> <h2 class="post-title"><a href="{% url "detail" id=post.id %}">{{ post.title }}</a></h2>
<p class="post-meta"> Time: <a class="post-author" href="#">{{ post.date_time |date:"Y /m /d"}}</a> <a class="post-category post-category-js" href="#">{{ post.category }}</a> </p> </header> </section> {% endfor %} </div><!-- /.blog-post --> {% endblock %}
|
并在my_blog/my_blog/urls.py
中添加对应url配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from django.conf.urls import url from django.contrib import admin from article import views
urlpatterns = [ # Examples: # url(r'^$', 'my_blog.views.home', name='home'), # url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)), url(r'^$', views.home, name = 'home'), url(r'^(?P<id>\d+)/$', views.detail, name='detail'), url(r'^archives/$', views.archives, name = 'archives'), ]
|
最终的效果如图。
AboutMe
这个就不多说了
在my_blog/my_blog/view.py下添加新的逻辑
1 2 3 4 5 6 7 8 9 10 11 12
| def about_me(request) : return render(request, 'aboutme.html') 在my_blog/template下新建模板aboutme.html, 内容如下, 大家可以自定义自己喜欢的简介
{% extends "base.html" %} {% load custom_markdown %}
{% block content %} <div class="posts"> <p> About Me ... </p> </div><!-- /.blog-post --> {% endblock %}
|
并在my_blog/my_blog/usls.py中添加对应url配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| from django.conf.urls import url from django.contrib import admin from article import views
urlpatterns = patterns('', # Examples: # url(r'^$', 'my_blog.views.home', name='home'), # url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)), url(r'^$', 'article.views.home', name = 'home'), url(r'^(?P<id>\d+)/$', views.detail, name='detail'), url(r'^archives/$', views.archives, name = 'archives'), url(r'^aboutme/$', views.about_me, name = 'about_me'), )
|
最终效果
标签分类
实现功能: 点击对应的标签按钮, 会跳转到一个新的页面, 这个页面是所有相关标签的文章的罗列
只需要在在my_blog/atricle/views.py下添加新的逻辑
1 2 3 4 5 6
| def search_tag(request, tag) : try: post_list = Article.objects.filter(category = tag) #contains except Article.DoesNotExist : raise Http404 return render(request, 'tag.html', {'post_list' : post_list})
|
可以看成是对tag的查询操作, 通过传入对应点击的tag, 然后对tag进行查询
1
| 在home.html,post.html,archive.html 文件中将tag对应的超链接代码修改为:"{% url "search_tag" tag=post.category %}"
|
在template文件夹中创建新的tag.html文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| {% extends "base.html" %}
{% load custom_markdown %} {% block content %} <div class="posts"> {% for post in post_list %} <section class="post"> <header class="post-header"> <h2 class="post-title"><a href="{% url "detail" id=post.id %}">{{ post.title }}</a></h2>
<p class="post-meta"> Time: <a class="post-author" href="#">{{ post.date_time |date:"Y M d"}}</a> <a class="post-category post-category-js" href="{% url "search_tag" tag=post.category %}">{{ post.category|title }}</a> </p> </header>
<div class="post-description"> <p> {{ post.content|custom_markdown }} </p> </div> <a class="pure-button" href="{% url "detail" id=post.id %}">Read More >>> </a> </section> {% endfor %} </div><!-- /.blog-post --> {% endblock %}
|
1
| 仔细看这一句<a class="post-category post-category-js" href="{% url "search_tag" tag=post.category %}">{{ post.category|title }}</a>. 其中标签对超链接已经发生改变, ```
|
这是在对标签就行点击时, 会将标签作为参数, 传入到对应的view中执行逻辑, 然后进行网页跳转…
并在my_blog/my_blog/usls.py中添加对应url配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| from django.conf.urls import url from django.contrib import admin from article import views
urlpatterns = patterns('', # Examples: # url(r'^$', 'my_blog.views.home', name='home'), # url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)), url(r'^$', views.home, name = 'home'), url(r'^(?P<id>\d+)/$', views.detail, name='detail'), url(r'^archives/$', views.archives, name = 'archives'), url(r'^aboutme/$', views.about_me, name = 'about_me'), url(r'^tag(?P<tag>\w+)/$', views.search_tag, name = 'search_tag'), )
|
现在在浏览器中输入http://localhost:9000/, 点击对应的归档或者ABOUT ME 或者标签按钮可以看到对应的效果。
我们在admin中添加了一个新的Article对象,并且这个Article对象的 category 是 C。
我们点击 红色的 Python 的 tag。
我们看到 URL 发生了变化,并且页面上只显示了 tag 是 Python 的 Article 对象。