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

(编辑:jimmy 日期: 2024/9/26 浏览:2)

命名 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 数据</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 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/("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 数据</a>
</body>
</html>

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

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

点击 “json 数据”

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

反向解析需要参数的话:

urls.py:

from django.conf.urls import url, include
from app01 import views
urlpatterns = [
 url(r'^test/', views.test),
 url(r'^json_test/("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/

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。