知識點(diǎn)
各個(gè)小模塊點(diǎn)擊以后有可能會(huì)發(fā)生跳轉(zhuǎn)到對應(yīng)的頁面,這里主要有3個(gè)小模塊臂寝,標(biāo)簽云章鲤、文章歸檔和文章的詳情頁面需要添加。主要思路是咆贬,點(diǎn)擊后發(fā)生請求败徊,后端獲取到前端的請求再做出響應(yīng)。
1 標(biāo)簽云詳情頁
前端請求的href【tags.html】
<li><a href='{% url "biaoqian" %}?tag={{ tag }}'>{{ tag }}</a></li>
請求到了路由【urls.py】
from django.conf.urls import url
from blog.views import biaoqian
urlpatterns = [
url(r'^biaoqian$', biaoqian, name='biaoqian'),
]
路由將請求轉(zhuǎn)給視圖【views.py】
def biaoqian(request):
try:
# 獲取請求的tag參數(shù)
tag_name = request.GET.get('tag', None)
# 找到tag所對應(yīng)的Tag對象
tag_obj = Tag.objects.get(name=tag_name)
# 使用_set通過一對多關(guān)系進(jìn)行查找
article_list = tag_obj.article_set.all()
except Exception as e:
logging.error(e)
return render(request, 'biaoqian.html', locals())
視圖將數(shù)據(jù)傳遞給前段模板渲染【biaoqian.html】
{% extends 'base.html' %}
{% load staticfiles %}
{% block left_content %}
{% include 'baseblock/ads.html' %}
<!-- banner代碼 結(jié)束 -->
<div class="topnews">
<h2>{{ request.GET.tag }} 標(biāo)簽云</h2>
{% for article in article_list%}
<div class="blogs">
<ul>
<h3><a href="/">{{ article.title }}</a></h3>
<p>{{ article.desc }}</p>
<p class="autor">
<span class="lm f_l">
{% for tag in article.tag.all %}
<a href="/">{{ tag.name }}</a>
{% endfor %}
</span>
<span class="dtime f_l">{{ article.date_publish | date:'Y-m-d' }}</span>
<span class="viewnum f_r">瀏覽(<a href="/">{{ article.click_count }}</a>)</span>
<span class="pingl f_r">評論(<a href="/">{{ article.comment_set.all.count }}</a>)</span>
</p>
</ul>
</div>
{% endfor %}
</div>
{% endblock %}
2 文章歸檔詳情頁(方法與標(biāo)簽云類似)
前端請求的href【sorts.html】
silce切片類似于[:4]掏缎,獲取需要的數(shù)據(jù)
<li><p><span class="tutime font-size-18"><a href='{% url "archive" %}?year={{ article_date | slice:":4" }}&month={{ article_date | slice:"7:9" }}'>{{ article_date }}</a></span></p></li>
請求轉(zhuǎn)到路由【urls.py】
from django.conf.urls import url
from blog.views import archive
urlpatterns = [
url(r'^archive$', archive, name='archive'),
]
路由將請求轉(zhuǎn)給視圖【views.py】
def archive(request):
try:
# 先獲取用戶提交的year和month信息
year = request.GET.get('year', None)
month = request.GET.get('month', None)
# 通過filter過濾出對應(yīng)年份的數(shù)據(jù)(icontains是包含)
article_list = Article.objects.filter(date_publish__icontains=year + '-' + month)
except Exception as e:
logging.error(e)
return render(request, 'archive.html', locals())
視圖將數(shù)據(jù)傳遞給前段模板渲染【atchive.html】
{% extends 'base.html' %}
{% load staticfiles %}
{% block left_content %}
{% include 'baseblock/ads.html' %}
<!-- banner代碼 結(jié)束 -->
<div class="topnews">
<h2>{{ request.GET.year }}年{{ request.GET.month }}月 歸檔文章</h2>
{% for article in article_list%}
<div class="blogs">
<ul>
<h3><a href="/">{{ article.title }}</a></h3>
<p>{{ article.desc }}</p>
<p class="autor">
<span class="lm f_l">
{% for tag in article.tag.all %}
<a href="/">{{ tag.name }}</a>
{% endfor %}
</span>
<span class="dtime f_l">{{ article.date_publish | date:'Y-m-d' }}</span>
<span class="viewnum f_r">瀏覽(<a href="/">{{ article.click_count }}</a>)</span>
<span class="pingl f_r">評論(<a href="/">{{ article.comment_set.all.count }}</a>)</span>
</p>
</ul>
</div>
{% endfor %}
</div>
{% endblock %}
3 文章詳情頁面設(shè)計(jì)
前端請求的href【sorts.html】
總共有3個(gè)地方:博客首頁皱蹦、文章排行、歸檔詳情頁面
## index.html【博客首頁】
<h3><a href='{% url "article" %}?id={{ article.id }}'>{{ article.title }}</a></h3>
## chart.html【文章排行】
<li><a href='{% url "article" %}?id={{ article_click.id }}' target="_blank">{{ article_click }}</a></li>
<li><a href='{% url "article" %}?id={{ article_comment.id }}' target="_blank">{{ article_comment }}</a></li>
<li><a href='{% url "article" %}?id={{ article_recommend.id }}' target="_blank">{{ article_recommend }}</a></li>
## archive.html【歸檔詳情頁面】
<h3><a href='{% url "article" %}?id={{ article.id }}'>{{ article.title }}</a></h3>
請求轉(zhuǎn)到路由【urls.py】
from django.conf.urls import url
from blog.views import article
urlpatterns = [
url(r'^article/$', article, name='article'),
]
路由將請求轉(zhuǎn)給視圖【views.py】
# 文章詳情
def article(request):
try:
# 獲取文章id
id = request.GET.get('id', None)
try:
# 獲取文章信息
article = Article.objects.get(pk=id)
except Article.DoesNotExist:
return render(request, 'failure.html', {'reason': '沒有找到對應(yīng)的文章'})
except Exception as e:
print e
logging.error(e)
return render(request, 'article.html', locals())
app下面新建一個(gè)templatetags目錄自定義過濾器【myfilter.py】
# -*- coding: utf-8 -*-
from django import template
register = template.Library()
# 定義一個(gè)將日期中的月份轉(zhuǎn)換為大寫的過濾器眷蜈,如8轉(zhuǎn)換為八
@register.filter
def month_to_upper(key):
return ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'][key.month-1]
視圖將數(shù)據(jù)傳遞給前段模板渲染【article.html】
這里使用了自定義的過濾器獲取對應(yīng)月份的大寫
{% extends 'base.html' %}
{% load staticfiles %}
{% load myfilter %}
{% block left_content %}
<div class="postdate">
<div class="month">{{ article.date_publish | month_to_upper }}</div>
<div class="date">{{ article.date_publish | date:'d' }}</div>
</div>
<div class="title">
<h2><a href="" rel="bookmark" title="{{ article.title }}">{{ article.title }}</a></h2>
<div class="postmeta">
<span class="postmeta_author">{{ article.user.username }}</span>
<span class="postmeta_category"><a href="" rel="category">{{ article.category.name }}</a></span>
<span class="postmeta_time">{{ article.date_publish | date:'Y-m-d' }}</span>
</div>
</div>
<div class="entry">
{{ article.content | safe }}
</div>
<span class="tags">
{% for tag in article.tag.all %}
<a href="?tag={{ tag.name }}" rel="tag">{{ tag.name }}</a>
{% endfor %}
</span>
{% endblock %}
相關(guān)下載
歡迎留言沪哺,博文會(huì)持續(xù)更新~~