文章目錄
- 0.思路引導
- 1.回顧
- 2.歸檔頁面
- 3.分類頁面
- 4.標簽頁面
0.思路引導
側邊欄已經正確地顯示了最新文章列表、歸檔、分類、標簽等信息,現在來完善歸檔、分類和標簽功能。
當用戶點擊歸檔下的某個日期、分類欄目下的某個分類或者標簽欄目下的某個標簽時,跳轉到文章列表頁面,顯示該日期、分類或者標簽下的全部文章。
1.回顧
首頁index.html繼承base.html,當首頁加載時,base.html中的模板標簽{% show_recent_post %}、 {% show_archieves %}、 {% show_categories %}、{% show_tages %},即在blog_extra.py中定義的模板標簽,從數據庫中獲取數據,
同時對模板標簽中參數指向的模板進行渲染,返回首頁內容,展示給讀者。
其中“最近文章”的跳轉地址通過get_post_url獲取,在上一篇文章中已經說明,本篇將對“歸檔”、“分類”、“標簽頁”進行點擊跳轉的相關操作。
2.歸檔頁面
1)首先,在 inclusions 文件夾下找到 archives 的模板,修改超鏈接的 href 屬性,讓用戶點擊超鏈接后跳轉到文章歸檔頁面:
文件位置:templates/blog/inclusions/_archives.html
...
{% for date in date_list %}
<li><a href="{% url 'blog:archive' date.year date.month %}">{{ date.year }} 年 {{ date.month }} 月</a>
</li>
{% endfor %}
...
這里 {% url %} 這個模板標簽的作用是解析視圖函數 blog:archive 對應的 URL 模式,并把 URL 模式中的年和月替換成 date.year,date.month 的值。
{% url %} 模板標簽接收的第一個參數為被解析視圖函數的端點值,這個端點值由 2 部分組成,中間由冒號分隔。第一部分為在應用的 urls.py 中指定的 app_name 的值(充當命名空間,這樣即使不同 app 下有相同的視圖函數名,也不會沖突),第二部分 path 函數中傳入的 name 參數的值。比如在 blog 應用的 urls.py 模塊,我們指定了 app_name = ‘blog’,archive 視圖函數的 url 模式為 path(‘archives/int:year/int:month/’, views.archive, name=‘archive’),因此對應的端點值為 blog:archive。
{% url %} 模板標簽接收的其它參數為 URL 路徑參數,即 URL 模式中路徑參數轉換器需要捕獲的值。例如 archive 視圖函數對應的 URL 模式為 archives/int:year/int:month/,假設 date.year=2017,date.month=5,那么 {% url ‘blog:archive’ date.year date.month %} 模板標簽返回的值為 /archives/2017/5/。
為什么要使用 {% url %} 模板標簽呢?事實上,我們把超鏈接的 href 屬性設置為 /archives/{{ date.year }}/{{ date.month }}/ 同樣可以達到目的,但是這種寫法是硬編碼的。雖然現在 blog:archive 視圖函數對應的 URL 模式是這種形式,但是如果哪天這個模式改變了呢?如果使用了硬編碼的寫法,那你需要把每一處 /archives/{{ date.year }}/{{ date.month }}/ 修改為新的模式。但如果使用了 {% url %} 模板標簽,則不用做任何修改。
2)其次,對路由進行設置:
文件位置:blog/urls.py
from django.urls import pathfrom . import viewsapp_name = 'blog'
urlpatterns = [path('', views.index, name='index'),path('posts/<int:pk>/', views.detail, name='detail'),path('archives/<int:year>/<int:month>/', views.archive, name='archive'),
]
這個歸檔視圖對應的 URL 和 detail 視圖函數對應的 URL 是類似的.
django 會從用戶訪問的 URL 中自動提取 URL 路徑參數轉換器 type:name 規則捕獲的值,然后傳遞給其對應的視圖函數。
例如如果用戶想查看 2017 年 3 月下的全部文章,他訪問 /archives/2017/3/,那么 URL 轉換器就會根據規則捕獲到 2017 和 3 這兩個整數,然后作為參數傳給 archive 視圖函數, archive 視圖函數的實際調用為:archive(request, year=2017, month=3)。
3)編寫視圖函數:
文件位置:blog/views.py
from .models import Postdef archive(request, year, month):post_list = Post.objects.filter(created_time__year=year,created_time__month=month).order_by('-created_time')return render(request, 'blog/index.html', context={'post_list': post_list})
這里使用了模型管理器(objects)的 filter 方法來過濾文章。由于是按照日期歸檔,因此這里根據文章發表的年和月來過濾。具體來說,就是根據 created_time 的 year 和 month 屬性過濾,篩選出文章發表在對應的 year 年和 month 月的文章。
注意這里 created_time 是 Python 的 date 對象,其有一個 year 和 month 屬性,Python 中調用屬性的方式通常是 created_time.year,但是由于這里作為方法的參數列表,所以 django 要求我們把點替換成了兩個下劃線,即 created_time__year。
同時和 index 視圖中一樣,我們對返回的文章列表進行了排序。此外由于歸檔頁面和首頁展示文章的形式是一樣的,因此直接復用了 index.html 模板。
3.分類頁面
同樣的邏輯,接下來對分類頁面和標簽頁面進行補充。
文件位置:templates/blog/inclusions/_categories.html
...
{% for category in category_list %}
<li><a href="{% url 'blog:category' category.pk %}">{{ category.name }}</a>
</li>
{% endfor %}
...
文件位置:blog/urls.py
urlpatterns = [path('archives/<int:year>/<int:month>/', views.archive, name='archive'),path('categories/<int:pk>/', views.category, name='category'),
]
文件位置:blog/views.py
# 引入 Category 類
from .models import Post, Categorydef category(request, pk):# 記得在開始部分導入 Category 類cate = get_object_or_404(Category, pk=pk)post_list = Post.objects.filter(category=cate).order_by('-created_time')return render(request, 'blog/index.html', context={'post_list': post_list})
4.標簽頁面
文件位置:templates/blog/inclusions/_tags.html
...
{% for tag in tag_list %}<li><a href="{% url 'blog:tag' tag.pk %}">{{ tag.name }}</a></li>
{% empty %}暫無標簽!
{% endfor %}
...
文件位置:blog/urls.py
from django.urls import pathfrom . import viewsapp_name = 'blog'
urlpatterns = [...path('categories/<int:pk>/', views.category, name='category'),path('tags/<int:pk>/', views.tag, name='tag'),
]
文件位置:blog/views.py
from .models import Category, Post, Tagdef tag(request, pk):# 記得在開始部分導入 Tag 類t = get_object_or_404(Tag, pk=pk)post_list = Post.objects.filter(tags=t).order_by('-created_time')return render(request, 'blog/index.html', context={'post_list': post_list})