Django命名URL和反向解析URL實(shí)現(xiàn)解析
命名 URL:
test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>測試頁面</title> </head> <body> <p>測試頁面</p> <form action="/test/" method="post"> <input type="text" name="username" value=""> <input type="submit" name="提交"> </form> <a href="/json_test/" rel="external nofollow" >json 數(shù)據(jù)</a> </body> </html>
urls.py:
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^test/', views.test), url(r'^json_test/', views.json_test), ]
如果 urls.py 中的 json_test/ 路徑發(fā)生改變,test.html 中的地址也要改
可以使用反向 url 解析,給 json_test/ 起一個別名
urls.py:
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^test/', views.test), url(r'^json_test/', views.json_test, name="json"), # 給該 url 匹配命名為 json ]
test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>測試頁面</title> </head> <body> <p>測試頁面</p> <form action="/test/" method="post"> <input type="text" name="username" value=""> <input type="submit" name="提交"> </form> <a href="{% url 'json' %}" rel="external nofollow" >json 數(shù)據(jù)</a> </body> </html>
這時候如果修改 urls.py 中的 json_test/ 路徑,就不需要再去修改 test.html
反向解析 URL:
如果需要重定向這樣的路徑的話,可以在 views.py 中這樣寫:
from django.shortcuts import render, redirect from django.urls import reverse # json 測試 def json_test(request): hobby = ["Music", "Movie", "Basketball", "Reading"] from django.http import HttpResponse, JsonResponse return JsonResponse(hobby, safe=False) def test(request): return redirect(reverse("json")) # 通過 json 反向得到路徑 json_test/
訪問:http://127.0.0.1:8000/test/ 就變成訪問:http://127.0.0.1:8000/json_test/
如果 url 需要傳參數(shù)的話:
urls.py:
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^test/', views.test), url(r'^json_test/(?P<id>[0-9]{2,4})/(?P<title>[a-zA-Z]+)/', views.json_test, name="json"), ]
test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>測試頁面</title> </head> <body> <p>測試頁面</p> <form action="/test/" method="post"> <input type="text" name="username" value=""> <input type="submit" name="提交"> </form> <a href="{% url 'json' 12 'abcd' %}" rel="external nofollow" >json 數(shù)據(jù)</a> </body> </html>
訪問:http://127.0.0.1:8000/test/
點(diǎn)擊 “json 數(shù)據(jù)”
反向解析需要參數(shù)的話:
urls.py:
from django.conf.urls import url, include from app01 import views urlpatterns = [ url(r'^test/', views.test), url(r'^json_test/(?P<id>[0-9]{2,4})/(?P<title>[a-zA-Z]+)/', views.json_test, name="json"), ]
views.py:
from django.shortcuts import HttpResponse, redirect from django.urls import reverse def json_test(request, id, title): print("id: ", id) print("title: ", title) return HttpResponse(id+"----"+title) def test(request): return redirect(reverse("json", kwargs={"id": 23, "title": "aaaa"}))
訪問:http://127.0.0.1:8000/test/
跳轉(zhuǎn)到了:http://127.0.0.1:8000/json_test/23/aaaa/
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實(shí)例
今天小編就為大家分享一篇python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python不改變Excel單元格樣式方式—xls和xlsx兩種格式
這篇文章主要介紹了Python不改變Excel單元格樣式方式—xls和xlsx兩種格式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實(shí)現(xiàn)
這篇文章主要介紹了opencv函數(shù)threshold、adaptiveThreshold、Otsu二值化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03詳解如何在ChatGPT內(nèi)構(gòu)建一個Python解釋器
這篇文章主要為大家詳細(xì)介紹了如何在ChatGPT內(nèi)構(gòu)建一個Python解釋器,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,需要的可以參考一下2023-02-02Python操作redis實(shí)例小結(jié)【String、Hash、List、Set等】
這篇文章主要介紹了Python操作redis的常見方法,結(jié)合實(shí)例形式總結(jié)分析了Python redis操作中String、Hash、List、Set等相關(guān)操作函數(shù)與使用技巧,需要的朋友可以參考下2019-05-05Python實(shí)現(xiàn)手機(jī)號自動判斷男女性別(實(shí)例解析)
這篇文章主要介紹了Python實(shí)現(xiàn)手機(jī)號自動判斷男女性別,本文性別判斷主要依靠airtest中的自動化測試實(shí)現(xiàn),通過實(shí)例代碼給大家講解的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12