Django 個(gè)人博客 - 詳情頁面 - step7

知識點(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ù)更新~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市酌儒,隨后出現(xiàn)的幾起案子辜妓,更是在濱河造成了極大的恐慌,老刑警劉巖忌怎,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件籍滴,死亡現(xiàn)場離奇詭異,居然都是意外死亡榴啸,警方通過查閱死者的電腦和手機(jī)孽惰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鸥印,“玉大人勋功,你說我怎么就攤上這事腥例。” “怎么了酝润?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵燎竖,是天一觀的道長。 經(jīng)常有香客問我要销,道長构回,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任疏咐,我火速辦了婚禮纤掸,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘浑塞。我一直安慰自己借跪,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布酌壕。 她就那樣靜靜地躺著掏愁,像睡著了一般。 火紅的嫁衣襯著肌膚如雪卵牍。 梳的紋絲不亂的頭發(fā)上果港,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天,我揣著相機(jī)與錄音糊昙,去河邊找鬼辛掠。 笑死,一個(gè)胖子當(dāng)著我的面吹牛释牺,可吹牛的內(nèi)容都是我干的萝衩。 我是一名探鬼主播,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼没咙,長吁一口氣:“原來是場噩夢啊……” “哼猩谊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起镜撩,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤预柒,失蹤者是張志新(化名)和其女友劉穎队塘,沒想到半個(gè)月后袁梗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡憔古,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年遮怜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸿市。...
    茶點(diǎn)故事閱讀 40,742評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡锯梁,死狀恐怖即碗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情陌凳,我是刑警寧澤剥懒,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站合敦,受9級特大地震影響初橘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜充岛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一保檐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧崔梗,春花似錦夜只、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至谈为,卻和暖如春砸王,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背峦阁。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工谦铃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人榔昔。 一個(gè)月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓驹闰,卻偏偏與公主長得像,于是被迫代替她去往敵國和親撒会。 傳聞我的和親對象是個(gè)殘疾皇子嘹朗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內(nèi)容