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

yipeiwu_com5年前Python基础

命名 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/(?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 数据</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/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

django 使用 PIL 压缩图片的例子

在最近做项目时,发现服务器上的图片比较大,数据传输时会消耗很多流量,体验非常不好。为了缓解这一现象,决定使用gzip压缩数据流,但是发现gzip对于json数据的压缩效果很好,但对于图片...

闭包在python中的应用之translate和maketrans用法详解

相对来说python对字符串的处理是比较高效的,方法也有很多。其中maketrans和translate两个方法被应用的很多,本文就针对这两个方法的用法做一总结整理。 首先让我们先回顾下...

pytorch + visdom CNN处理自建图片数据集的方法

pytorch + visdom CNN处理自建图片数据集的方法

环境 系统:win10 cpu:i7-6700HQ gpu:gtx965m python : 3.6 pytorch :0.3 数据下载 来源自Sasank Chilamkurthy 的...

python里使用正则的findall函数的实例详解

python里使用正则的findall函数的实例详解 在前面学习了正则的search()函数,这个函数可以找到一个匹配的字符串返回,但是想找到所有匹配的字符串返回,怎么办呢?其实得使用f...

Python requests发送post请求的一些疑点

Python requests发送post请求的一些疑点

前言 在Python爬虫中,使用requests发送请求,访问指定网站,是常见的做法。一般是发送GET请求或者POST请求,对于GET请求没有什么好说的,而发送POST请求,有很多朋友不...