首先看前端, 這里只列出了重點(diǎn)博肋,就是其中的a標(biāo)簽
{% for post in posts %}
<p style="font-family: '微軟雅黑 Light'; font-size: 16pt; font-weight: bold;">
<a href="/post/{{ post.slug }}">{{ post.title }}</a>
</p>
再看urls.py中
要使用正則表達(dá)式的路由,要新引入一個(gè)量:re_path
再看views.py
獲取數(shù)據(jù)庫(kù)數(shù)據(jù)渗饮,返回到前端
def showpost(req, slug):
template = get_template("post.html")
try:
post = models.Post.objects.get(slug=slug)
if post != None:
html = template.render(locals())
return HttpResponse(html)
except:
return redirect('/') # 返回首頁(yè)