django3.02模板中的超鏈接配置實(shí)例代碼
1.在myblog中的urls.py中
from django.urls import include
from django.conf.urls import url
urlpatterns = [
path('blog/',include('blog.urls')),
]
2.在blog的urls.py中
from django.urls import path
from django.conf.urls import url
from . import views
urlpatterns = [
path('index',views.index),
path('article/<int:article_id>',views.article_page,name='article_page')
]
3.在blog的view.py中
from django.shortcuts import render
from django.http import HttpResponse
from . import models
# Create your views here.
def index(request):
articles = models.Article.objects.all()
return render(request, 'blog/index.html', {'articles': articles})
def article_page(request,article_id):
article = models.Article.objects.get(pk=article_id)
return render(request,'blog/article_page.html',{'article':article})
#redner的第三個(gè)參數(shù)是用來(lái)傳遞數(shù)據(jù)到前端的,函數(shù)中支持一個(gè)disc參數(shù)(字典類型的數(shù)據(jù))
4.在blog/templates/blog/index中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<h1><a href="">新文章</a></h1>
{% for article in articles %}
<a href="/blog/article/{{article.id}}" rel="external nofollow" >{{article.title}}</a>
<br/>
{% endfor %}
</body>
</html>
5.在blog/templates/blog/article_page.html中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>article page</title>
</head>
<body>
<h1>{{article.title}}</h1>
<br/>
<h3>{{article.content}}</h3>
<br/><br/>
<a href="">修改文章</a>
</body>
</html>
以上代碼大家可以在本地測(cè)試下,如果有任何補(bǔ)充可以聯(lián)系腳本之家小編。
相關(guān)文章
Python使用SocketServer模塊編寫基本服務(wù)器程序的教程
SocketServer模塊中集成了實(shí)現(xiàn)socket通信服務(wù)器功能所需的各種類和方法,這里我們就來(lái)看一下Python使用SocketServer模塊編寫基本服務(wù)器程序的教程:2016-07-07
python實(shí)現(xiàn)Thrift服務(wù)端的方法
這篇文章主要介紹了python實(shí)現(xiàn)Thrift服務(wù)端的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
python 實(shí)現(xiàn)交換兩個(gè)列表元素的位置示例
今天小編就為大家分享一篇python 實(shí)現(xiàn)交換兩個(gè)列表元素的位置示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-06-06
python樹狀打印項(xiàng)目路徑的實(shí)現(xiàn)
在Python中,要打印當(dāng)前路徑,可以使用os模塊中的getcwd()函數(shù),本文主要介紹了python樹狀打印項(xiàng)目路徑,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Python實(shí)現(xiàn)對(duì)字符串的加密解密方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)對(duì)字符串的加密解密方法,結(jié)合實(shí)例形式分析了Python使用PyCrypto模塊進(jìn)行DES加密解密的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
python中對(duì)正則表達(dá)式re包的簡(jiǎn)單引用方式
這篇文章主要介紹了python中對(duì)正則表達(dá)式re包的簡(jiǎn)單引用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

