Django命名URL和反向解析URL实现解析

每到春天,红得如火的木棉花,粉得如霞的芍药花,白得如玉的月季花竞相开放。它们有的花蕾满枝,有的含苞初绽,有的昂首怒放。一阵沁人心肺的花香引来了许许多多的小蜜蜂,嗡嗡嗡地边歌边舞。

命名 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 rel="nofollow noopener noreferrer" href="/json_test/" rel="external nofollow" >json 数据</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/ 路径发生改变,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 rel="nofollow noopener noreferrer" href="{% url 'json' %}" rel="external nofollow" >json 数据</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 需要传参数的话:

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 rel="nofollow noopener noreferrer" href="{% url 'json' 12 'abcd' %}" rel="external nofollow" >json 数据</a>
</body>
</html>

访问:http://127.0.0.1:8000/test/

点击 “json 数据”

反向解析需要参数的话:

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/

跳转到了:http://127.0.0.1:8000/json_test/23/aaaa/

到此这篇关于Django命名URL和反向解析URL实现解析就介绍到这了。热情和欲望可以突破一切难关。更多相关Django命名URL和反向解析URL实现解析内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

标签: Django URL