Django命名URL和反向解析URL實現(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/

點擊 “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)文章
Flask搭建虛擬環(huán)境并運行第一個flask程序
這篇文章主要介紹了Flask搭建虛擬環(huán)境并運行第一個flask程序,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Python利用jmespath模塊進行json數(shù)據(jù)處理
jmespath是python的第三方模塊,是需要額外安裝的。它在python原有的json數(shù)據(jù)處理上做出了很大的貢獻。本文將詳細介紹如何利用jmespath實現(xiàn)json數(shù)據(jù)處理,需要的可以參考一下2022-03-03
使用python和Django完成博客數(shù)據(jù)庫的遷移方法
下面小編就為大家分享一篇使用python和Django完成博客數(shù)據(jù)庫的遷移方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

