django之反向解析和命名空间
背景:当我们页面中存放的请求路径与url文件中的url一致时,如果url改了是不是所有的请求路径都要跟着改?显然不现实,这里我们就要用到反向解析。
如下图所示,输入url后会跳转到登录页面,输入用户名密码后跳转到欢迎页面。
# 在公共项目中urls中的url from django.contrib import admin
from django.urls import path, re_path, include
from website import views urlpatterns = [
path('admin/', admin.site.urls),
path('data/', views.data),
path('login./', views.login),
] # 在应用中views文件中的视图函数
from django.shortcuts import render, HttpResponse def login(request):if request.method == 'GET':
return render(request, 'login.html')
elif request.method == 'POST':
if request.POST.get('user') == 'admin' and request.POST.get('pwd') == '':
return HttpResponse('welcome to world')
else:
return HttpResponse('用户名或密码错误')
# 在公共项目统一目录层建的templates层存放页面文件 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static/js.js"> </script>
</head>
<body> <h4>
登录页面
</h4>> <form action="http://127.0.0.1:8000/login/" method="post">
用户名<input type="text" name="user">
密码<input type="password" name="pwd">
<input type="submit">
</form>
</body>
</html>
现在我需要把公共项目中的url改变,再次点击登录后报错了
urlpatterns = [
path('admin/', admin.site.urls),
path('data/', views.data),
path('login.html/', views.login),
]
报错如图所示:

因为我们登录时action中的路径找不到http://127.0.0.1:8000/login/路径,现在已经被改成了http://127.0.0.1:8000/login.html/,此时难道我们需要一直改这个路径吗?不存在的!
接下来到了反向解析的重点
在公共项目中的urls文件中把url加一个属性name
from django.contrib import admin
from django.urls import path, re_path, include
from website import views urlpatterns = [
path('admin/', admin.site.urls),
path('data/', views.data),
path('login.html/', views.login, name='hello')
]
在html中把action路径改为{{% url 'hello' %}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static/js.js"> </script>
</head>
<body> <h4>
登录页面
</h4>> <form action="{% url 'hello' %}" method="post">
用户名<input type="text" name="user">
密码<input type="password" name="pwd">
<input type="submit">
</form>
</body>
</html>
此时再次去访问页面就不会报错啦!
还有个反向解析办法就是导入reverse模块
在公共项目中的urls文件中
from django.contrib import admin
from django.urls import path, re_path, include
from website import views urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.login, name='hello'), # 路由配置 路径--------》视图函数 re_path(r'^articles/2003/', views.special_case_2003, name='one'),
re_path(r'^articles/([0-9]{4})/$', views.year_archive, name='two'),
re_path(r'^articles/(?P<y>[0-9]{4})/(?P<m>[[0-9]{2})/$', views.month),
在应用中的views文件中的视图函数
from django.shortcuts import render, HttpResponse
import time
from django.urls import reverse # Create your views here. def special_case_2003(request): return HttpResponse('special_case') def year_archive(request, year): return HttpResponse(year) def month(request, m, y): return HttpResponse(y+'+'+m) def login(request): print(reverse('one')) # reserve中放的是路径中的name
print(reverse('two', args=(4099,))) # 当有元祖时参数必须放在args中 if request.method == 'GET':
return render(request, 'login.html')
elif request.method == 'POST':
if request.POST.get('user') == 'admin' and request.POST.get('pwd') == '':
return HttpResponse('welcome to world')
else:
return HttpResponse('用户名或密码错误')
输入ur:http://127.0.0.1:8000/login/后,查看reserve中的路径为下图所示

这样就把反向解析的路径获取到了 !!!
此时有个问题就来了,如果在两个应用中的url相同且对应的name也相同,那会获取到哪个路径呢?经测试得出会获取到第二个,因为第一个被第二个覆盖了。那怎么解决呢?
如下图所示
# 公共项目中的url from django.contrib import admin
from django.urls import path, re_path, include
from website import views urlpatterns = [
path('admin/', admin.site.urls),
path('data/', views.data),
path('login/', views.login, name='hello'), # 路由配置 路径--------》视图函数 # 分发
re_path(r'website/', include(('website.urls', 'website'))),
re_path(r'website1/', include(('website1.urls', 'website1')))
# 在第一个项目中的url文件 from django.urls import path, re_path
from website import views urlpatterns = [ # 路由配置 路径--------》视图函数 re_path('index', views.index, name='index') ] # 在第一个项目中的views文件 from django.shortcuts import render, HttpResponse
from django.urls import reverse def index(request): return HttpResponse(reverse('website:index')) 这里返回第一个项目的index
# 在第二个项目中的url文件 from django.urls import path, re_path
from website1 import views urlpatterns = [ # 路由配置 路径--------》视图函数 re_path('index', views.index, name='index') ] # 在第二个项目中的views文件 from django.shortcuts import render, HttpResponse
from django.urls import reverse def index(request): return HttpResponse(reverse('website1:index')) # 这里返回第二个项目的index
请尊重作者劳动成果,有需要请转载,标明出处!!!
django之反向解析和命名空间的更多相关文章
- Django-url反向解析和命名空间
一.urls硬编码 在反向解析和命名空间之前我们先来说说URLS硬编码,用django 开发应用的时候,可以完全是在urls.py 中硬编码配置地址,在views.py中HttpResponseRed ...
- python 之 Django框架(路由系统、include、命名URL和URL反向解析、命名空间模式)
12.36 Django的路由系统 基本格式: from django.conf.urls import url urlpatterns = [ url(正则表达式, views视图函数,参数,别名) ...
- Django url反向解析与路由分发名称空间
url反向解析 url.py from django.conf.urls import url from django.contrib import admin from app01 import v ...
- Django【第2篇】:Django之反向解析
Django框架之第二篇 一.知识点回顾 1.MTV模型 model:模型,和数据库相关的 template:模板,存放html文件,模板语法(目的是将变量如何巧妙的嵌入到HTML页面中). view ...
- Django 的反向解析与有无名分组
无名分组(将加括号的正则表达式匹配到的内容当做位置参数自动传递给对应的视图函数) url(r'^test/(\d+)/',views.test), # 匹配一个或多个数字 def test(reque ...
- Django url配置 正则表达式详解 分组命名匹配 命名URL 别名 和URL反向解析 命名空间模式
Django基础二之URL路由系统 本节目录 一 URL配置 二 正则表达式详解 三 分组命名匹配 四 命名URL(别名)和URL反向解析 五 命名空间模式 一 URL配置 Django 1.11版本 ...
- Django正向解析和反向解析
转载:https://blog.csdn.net/jeekmary/article/details/79673867 先创建一个视图界面 urls.py index.html index页面加载的效果 ...
- django反向解析和正向解析
Django的正向解析和反向解析 先创建一个视图界面 urls.py index.html index页面加载的效果 正向解析 test/?result=1 所谓正向解析就是直接在这里写地址 向url ...
- django反向解析URL和URL命名空间
django反向解析URL和URL命名空间 首先明确几个概念: 1.在html页面上的内容特别是向用户展示的url地址,比如常见的超链接,图片链接等,最好能动态生成,而不要固定. 2.一个django ...
随机推荐
- python3练习100题——006
继续做题-经过py3测试 原题链接:http://www.runoob.com/python/python-exercise-example6.html 题目:斐波那契数列. 我的代码: def fi ...
- 第三十二篇 玩转数据结构——AVL树(AVL Tree)
1.. 平衡二叉树 平衡二叉树要求,对于任意一个节点,左子树和右子树的高度差不能超过1. 平衡二叉树的高度和节点数量之间的关系也是O(logn) 为二叉树标注节点高度并计算平衡因子 AVL ...
- L2-2 小字辈
思路 bfs搜一下. 代码 #include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; vector<i ...
- AcWing 154. 滑动窗口
https://www.acwing.com/problem/content/156/ #include <iostream> using namespace std; ; int a[N ...
- Mysql中的触发器【转】
转载:https://www.cnblogs.com/chenpi/p/5130993.html 阅读目录 什么是触发器 特点及作用 例子:创建触发器,记录表的增.删.改操作记录 弊端 什么是触发器 ...
- Flink流处理(一)- 状态流处理简介
1. Flink 简介 Flink 是一个分布式流处理器,提供直观且易于使用的API,以供实现有状态的流处理应用.它能够以fault-tolerant的方式高效地运行在大规模系统中. 流处理技术在当今 ...
- MySQL学习(十一)double write 介绍 (半原创)
复习 Innodb关键的特性 插入缓存 两次写 异步IO 刷新邻近页 自适应哈希索引 概述 double write 的主要的作用是保证写入数据库文件的可靠性.通俗地说就是一份数据写两个地方,当出现异 ...
- [Note]Splay学习笔记
开个坑记录一下学习Splay的历程. Code 感谢rqy巨佬的代码,让我意识到了Splay可以有多短,以及我之前的Splay有多么的丑... int fa[N], ch[N][2], rev[N], ...
- Bugku-一段Base64-Writeup
转载请注明出处:http://www.cnblogs.com/WangAoBo/p/7207874.html bugku - 一段Base64 - Writeup 题目: 分析: 本来看到题目名字和分 ...
- const与#define的区别、优点
const与#define的区别 编译器处理方式不同 define宏是在预处理阶段展开. 补充:预处理器根据以#开头的命令,修改原始的程序.比如我们常见的#include <stdio.h> ...