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?wasmtime調(diào)用rust生成的wasm庫(kù)
這篇文章主要介紹了如何使用python?wasmtime調(diào)用rust生成的wasm庫(kù),使用python wasmtime來(lái)訪問(wèn)rust庫(kù)的便捷方法,步驟極其簡(jiǎn)練,可以在生產(chǎn)環(huán)境中使用,需要的朋友可以參考下2023-01-01python機(jī)器學(xué)習(xí)XGBoost梯度提升決策樹的高效且可擴(kuò)展實(shí)現(xiàn)
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)XGBoost梯度提升決策樹的高效且可擴(kuò)展實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python如何按單元格讀取復(fù)雜電子表格(Excel)的數(shù)據(jù)
這篇文章主要介紹了Python如何按單元格讀取復(fù)雜電子表格(Excel)的數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Python批量對(duì)word文檔進(jìn)行操作步驟
這篇文章主要介紹了Python批量對(duì)word文檔進(jìn)行操作,一步步逐步完成創(chuàng)建文檔,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02python采集百度搜索結(jié)果帶有特定URL的鏈接代碼實(shí)例
這篇文章主要介紹了python采集百度搜索結(jié)果帶有特定URL的鏈接代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python PyQt5實(shí)現(xiàn)拖放效果的原理詳解
這篇文章主要為大家詳細(xì)介紹了Python PyQt5中拖放效果的實(shí)現(xiàn)原理與實(shí)現(xiàn)代碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11