Django入門:第九章、查看博客文章

Django入門:第九章、查看博客文章

回顧

上一章(Django入門:第八章、模板引擎設置)中,我們成功的把博客標題顯示出來,那麼如何才能查看文章內容,從功能上來說,點擊文章標題後呈現其詳細內容。

因此,文章標題需要做一個超鏈接,對象就是文章詳情頁。

修改home.html

templates/home.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>我的博客</title>
    </head>
    <body>
    <h1>我的博客</h1>
    {% for blog in blogs %}
        <li><a href="{{ blog.id }}">{{ blog.title }} </a></li> <br>
    {% endfor %}
    </body>
</html>

修改完成後,我們刷新頁面,發現每個標題就是一個超鏈接,當然,此時單擊,並不能顯示其詳情。如圖:

在這裏插入圖片描述

編輯./blog/views.py

./blog/views.py
我們增加響應查看博客文章內容請求的函數blog_article():

from django.shortcuts import render
from .models import BlogArticles


# Create your views here.
def home(request):
    blogs = BlogArticles.objects.all()
    return render(request, "home.html", {"blogs": blogs})


def blog_article(request, article_id):
    article = BlogArticles.objects.get(id=article_id)
    publish_time = article.publish
    return render(request, "content.html", {"article": article, "publish": publish_time})

在函數blog_ariticle(request, article_id)中,article_id參數的目的是爲了獲得URL中每篇博客文章的id

創建./templates/content.html

在templates目錄下,創建一個content.html文件,並寫入如下代碼:

./templates/content.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>
            {{ article.title }}
        </title>
    </head>

    <body>
    <h1 style="text-align: center">
        {{ article.title }}
    </h1>

    <div style="text-align: center">
        {{ article.author.username }}
        <span style="margin-left: 20px">
            {{ article.publish }}
        </span>
    </div>
    <div>
        {{ article.body }}
    </div>

    </body>
</html>

配置URL

myblog/urls.py中增加新的URL路徑:

myblog/urls.py*

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from blog import views


urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^$', views.home, name='home'),
    path('<int:article_id>/', views.blog_article),  # 新增
]

測試

我們此時此刻再單擊一下其中一個文章題目,比如雨霖鈴·寒蟬悽切,可以看到如下頁面:
在這裏插入圖片描述

注意

如果我們修改最後代表文章id的數字,如果改成9,將會出現如下頁面:
在這裏插入圖片描述

因爲不存id爲9的文章,所以報錯,而且顯示了錯誤的完整信息。

爲了避免出現上訴錯誤,我們應該在響應此請求的函數中對這種異常請求進行處理。適當修改./blog/view.py中的blog_article()函數。

from django.shortcuts import render, get_object_or_404
from .models import BlogArticles


# Create your views here.
def home(request):
    blogs = BlogArticles.objects.all()
    return render(request, "home.html", {"blogs": blogs})


def blog_article(request, article_id):
    # article = BlogArticles.objects.get(id=article_id)
    article = get_object_or_404(BlogArticles, id=article_id)
    publish_time = article.publish
    return render(request, "content.html", {"article": article, "publish": publish_time})

再次請求http://127.0.0.1:8000/9/,我們會發現在Debug模式下的(404)錯誤:
在這裏插入圖片描述

如果我們把 myblog/settings.py中的DEBUG = True,改爲 DEBUG = False,並且設置 ALLOWED_HOSTS = [‘127.0.0.1’],類似下圖:

myblog/settings.py

DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1']

再重新刷新這個頁面,將會看一個標準的404頁面,如圖:
在這裏插入圖片描述

總結

Django入門筆記到這一步,簡單的博客就搭建完成,雖然很簡陋,頁面不美觀,但顯示了Django在網站開發中的最基本結構。

我們學會了創建一個簡單的HelloWorld視圖,創建了第一個模型、遷移了數據庫,試用了Model API。同時還配置了Django模板引擎,希望大家多多練習,熟悉這個過程。

我們的優秀博客終於能向大家展示了,希望你能喜歡。



注:點Django入門筆記彙總博客查看目前已經更新的文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章