歸檔
歸檔就是列出當(dāng)前博客中所有的文章, 并且能夠顯示時(shí)間, 很容易的可以寫出對(duì)應(yīng)的view和模板來(lái)
在my_blog/my_blog/view下新建歸檔view
defarchives(request):try:? ? ? ? post_list = Article.objects.all()exceptArticle.DoesNotExist :raiseHttp404returnrender(request,'archives.html', {'post_list': post_list,'error':False})
在my_blog/templates新建模板archives.html
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
{{ post.title }}
Time:? {{ post.date_time |date:"Y /m /d"}} {{ post.category }}
{% endfor %}
{% endblock %}
并在my_blog/my_blog/usls.py中添加對(duì)應(yīng)url配置
fromdjango.conf.urlsimportpatterns, include, urlfromdjango.contribimportadminurlpatterns = 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\d+)/$','article.views.detail', name='detail'),? ? url(r'^archives/$','article.views.archives', name ='archives'),)
AboutMe
這個(gè)就不多說(shuō)了
在my_blog/my_blog/view.py下添加新的邏輯
def about_me(request) :
return render(request, 'aboutme.html')
在my_blog/template下新建模板aboutme.html, 內(nèi)容如下, 大家可以自定義自己喜歡的簡(jiǎn)介
{% extends "base.html" %}
{% load custom_markdown %}
{% block content %}
About Me 正在建設(shè)中
{% endblock %}
并在my_blog/my_blog/usls.py中添加對(duì)應(yīng)url配置
fromdjango.conf.urlsimportpatterns, include, urlfromdjango.contribimportadminurlpatterns = 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\d+)/$','article.views.detail', name='detail'),? ? url(r'^archives/$','article.views.archives', name ='archives'),? ? url(r'^aboutme/$','article.views.about_me', name ='about_me'),)
標(biāo)簽分類
實(shí)現(xiàn)功能: 點(diǎn)擊對(duì)應(yīng)的標(biāo)簽按鈕, 會(huì)跳轉(zhuǎn)到一個(gè)新的頁(yè)面, 這個(gè)頁(yè)面是所有相關(guān)標(biāo)簽的文章的羅列
只需要在在my_blog/my_blog/view.py下添加新的邏輯
def search_tag(request, tag) :
try:
post_list = Article.objects.filter(category__iexact = tag) #contains
except Article.DoesNotExist :
raise Http404
return render(request, 'tag.html', {'post_list' : post_list})
可以看成是對(duì)tag的查詢操作, 通過(guò)傳入對(duì)應(yīng)點(diǎn)擊的tag, 然后對(duì)tag進(jìn)行查詢
在對(duì)應(yīng)的有tag的html網(wǎng)頁(yè)中修改代碼
{% extends "base.html" %}
{% load custom_markdown %}
{% block content %}
{% for post in post_list %}
{{ post.title }}
Time:? {{ post.date_time |date:"Y M d"}} {{ post.category|title }}
{{ post.content|custom_markdown }}
{% endfor %}
{% endblock %}
仔細(xì)看這一句{{ post.category|title }}. 其中標(biāo)簽對(duì)超鏈接已經(jīng)發(fā)生改變, 這是在對(duì)標(biāo)簽就行點(diǎn)擊時(shí), 會(huì)將標(biāo)簽作為參數(shù), 傳入到對(duì)應(yīng)的view中執(zhí)行邏輯, 然后進(jìn)行網(wǎng)頁(yè)跳轉(zhuǎn)...
并在my_blog/my_blog/usls.py中添加對(duì)應(yīng)url配置
fromdjango.conf.urlsimportpatterns, include, urlfromdjango.contribimportadminurlpatterns = 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\d+)/$','article.views.detail', name='detail'),? ? url(r'^archives/$','article.views.archives', name ='archives'),? ? url(r'^aboutme/$','article.views.about_me', name ='about_me'),? ? url(r'^tag(?P\w+)/$','article.views.search_tag', name ='search_tag'),)
現(xiàn)在在瀏覽器中輸入http://127.0.0.1:8000/, 點(diǎn)擊對(duì)應(yīng)的歸檔或者ABOUT ME 或者標(biāo)簽按鈕可以看到對(duì)應(yīng)的效果