>
href
="}"
>
}a>
li>
獲取id為5的article:
知道了如何獲取指定id的article物件,現在先編輯blog/views.py
,增加響應檢視文章詳情的請求函式blog_article
:
def
blog_article
(request, article_id)
: article = blogarticles.objects.get(
id=article_id)
pub = article.publish
return render(request,
"blog/content.html"
,)
建立與之對應的模板:blog/template/blog/content.html
blog article
class
="row text-center vertical-middle-sm"
>
>
}h1>
div>
class
="row"
>
class
="col-xs-12 col-md-8"
>
class
="text-center"
>
>
}span
>
style
="margin-left
: 20px;
">
}span
>
>
}div
>
p>
div>
class
="col-xs-6 col-md-4"
>
>
廣告h2
>
>
讀書筆記p
>
width
="200px"
src=""
>
div>
div>
from django.conf.urls import url
from
.import views
urlpatterns =
[# r"^$" :表示訪問當前應用的根目錄,對應:
# views.blog_title :宣告了響應這個請求的函式物件
url(r"^$"
, views.blog_title, name=
"blog_title"),
url(
r"(?p\d)/$"
, views.blog_article, name=
"blog_detail"),
# 可以取到列表超連結賦的id值
]
一切完整,執行專案:
如果修改訪問url為6
,瀏覽器會顯示十分詳盡的錯誤資訊:
doesnotexist at /blog/6/
blogarticles matching query does not exist.
...
這是因為目前在debug模式下,錯誤資訊會盡可能的詳盡,便於除錯;
from django.shortcuts import render, get_object_or_404
# create your views here.
from
.models import blogarticles
defblog_title
(request)
: blogs = blogarticles.objects.
all(
)return render(request,
"blog/titles.html",)
defblog_article
(request, article_id)
:# article = blogarticles.objects.get(id=article_id)
article = get_object_or_404(blogarticles,
id=article_id)
pub = article.publish
return render(request,
"blog/content.html"
,)
再訪問id=6的url頁面,會顯示404頁面:
page not found (404)
request method: get
request url: 6/
raised by: blog.views.blog_article
類似的django中海油很對的預設方法,可以幫助提高開發效率,減少**量; Django5 簡訊驗證
from django.contrib import admin from django.urls import path,include urlpatterns path admin admin.site.urls path include from django.urls import path...
django 5 表單1 檔案上傳
上傳檔案1 class userform forms.form name forms.charfield headimg forms.filefield defregist req if req.method post uf userform req.post,req.files ifuf.is v...
Django5 資料庫之模型字段
欄位名型別 備註models.autofield 自增列 int 如果沒有的話,缺省會生成乙個名稱為 id 的列,如果要顯示的自定義乙個自增列,必須將給列設定為主 鍵 primary key true。models.charfield 字串字段 用於從資料庫層和django校驗層限制該字段所允許的最...