上一篇簡單的建立了詳情頁和首頁,這篇稍微優化一下,添加發布日期,分類,標籤以及根據標籤篩選該標籤的內容。html
一、優化首頁和詳情頁django
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{ blog.title }}</title> </head> <body> <a href="{% url 'home' %}"> <div> <h3>felix Blog</h3> </div> </a> <h3>{{ blog.title }}</h3> <p>做者:{{ blog.author }}</p> {# 時間過濾器讓時間按照本身須要的格式過濾 #} <p>發佈日期:{{ blog.created_time|date:"Y-m-d H:n:s" }}</p> <p>分類: <a href="{% url 'blogs_with_type' blog.blog_type.pk %}"> {{ blog.blog_type }} </a> </p> <p>{{ blog.content }}</p> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>felix Blog</title> </head> <body> <div> <h3>felix Blog</h3> </div> <br/> {% for blog in blogs %} <a href="{% url 'blog_detail' blog.pk %}"><h3>{{ blog.title }}</h3></a> {# 添加過濾器 文章太長時只顯示前30個字符 #} <p>{{ blog.content|truncatechars:30 }}</p> {% empty %} <p>--暫無博客,敬請期待--</p> {% endfor %} {# 過濾器統計博客數量 #} <p>一共有{{ blogs|length }}篇博客</p> </body> </html>
二、添加經過標籤篩選文章ide
在templates下新建blog_with_type.html文件測試
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{ blog_type.type_name }}</title> </head> <body> <div> <h3>felix Blog</h3> </div> <br/> <h3>標籤名:{{ blog_type.type_name }}</h3> {% for blog in blogs %} <a href="{% url 'blog_detail' blog.pk %}"><h3>{{ blog.title }}</h3></a> {# 添加過濾器 文章太長時只顯示前30個字符 #} <p>{{ blog.content|truncatechars:30 }}</p> {% empty %} <p>--暫無博客,敬請期待--</p> {% endfor %} {# 過濾器統計博客數量 #} <p>一共有{{ blogs|length }}篇博客</p> </body> </html>
三、新建視圖和路徑優化
def blogs_with_type(requests, blog_type_pk): blog_type = get_object_or_404(BlogType, pk=blog_type_pk) context = { 'blogs': Blog.objects.filter(blog_type=blog_type), 'blog_type':blog_type, } return render_to_response('blog_with_type.html', context)
from django.shortcuts import render_to_response, get_object_or_404 from .models import Blog, BlogType # Create your views here. # 博客列表 def blog_list(requests): context = { 'blogs': Blog.objects.all() } return render_to_response('blog_list.html', context) # 博客詳情 def blog_detail(requests, blog_pk): context = { 'blog': get_object_or_404(Blog, pk=blog_pk) } return render_to_response('blog_detail.html', context) def blogs_with_type(requests, blog_type_pk): blog_type = get_object_or_404(BlogType, pk=blog_type_pk) context = { 'blogs': Blog.objects.filter(blog_type=blog_type), 'blog_type':blog_type, } return render_to_response('blog_with_type.html', context)
# -*- coding: utf-8 -*- # @Time : 18-11-4 下午5:22 # @Author : Felix Wang from django.urls import path from . import views urlpatterns = [ # name 表示別名 path('<int:blog_pk>',views.blog_detail,name='blog_detail'), # 連接很是容易混掉,因此要區分 path('type/<int:blog_type_pk>',views.blogs_with_type,name='blogs_with_type'), ]
四、個人項目目錄ui
最近發現得添加本身的項目目錄,以分別出和上一篇博客相比,那些文件產生了變化。url
其中:spa
紅色表示新建的文件3d
藍色表示修改過得文件code
五、啓動服務測試
若是博客數據不夠多,能夠進入admin後臺管理中建立,好比多建立一些標籤和一些博客,看效果。
六、其餘只是補充
(1)、模板的經常使用過濾器
日期:date
字數截取:truncatechars、truncatechars_html、truncatewords、truncatewords_html
是否信任html:safe
長度:length
參考連接:https://docs.djangoproject.com/en/2.1/ref/templates/builtins/
(2)、經常使用的模板標籤
循環:for 條件:if、ifequal、ifnotequal 連接:url 模板嵌套:block、extends、include 註釋:{# #}