您好,登录后才能下订单哦!
本篇文章给大家分享的是有关Django中怎么反向生成url,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
1.客户端的浏览器发起一个url请求,Django根据URL解析,把url中的参数捕获,调用相应的试图,获取相应的数据,然后返回给客户端显示
2.通过一个视图的名字,再加上一些参数和值,逆向获取相应的URL
第一个就是平常的请求有URLconf来解析的过程,
第二个叫做,url的逆向解析,url逆向匹配,url的逆向查阅,等
Django提供了不同的层级的url逆向处理工具:
1.在模板templates中,使用url标记,如:{% url %}
2.在Python代码中,使用django.core.urlresolvers.reverse()方法
3.在更高一层级的处理url中,用get_absolute_url()方法
新建一个项目diango
访问longin页面跳转到 index页面
app01 views.py
from django.shortcuts import render,redirect,HttpResponse def index(request): return HttpResponse('Index') def login(request): return HttpResponse('longin')
urls.py
from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^index/', views.index), url(r'^login/', views.login), ]
在平常做法
views.py
views.py def login(request): return redirect('/index/')
如果usr 很长
urlpatterns = [ url(r'^index/xxxxx/xxx/xxx/xxx/xx/', views.index), url(r'^login/', views.login), ]
可以用平常做法:
app01 views.py
def login(request): return redirect('/index/xxxxx/xxx/xxx/xxx/xx/')
这种方法看上去不是那么的友好, 我们用usl 别名的方式
导入reverse
app01/views.py
from django.urls import reverse def login(request): url = ('inx') return redirect(url)
urls.py
urlpatterns = [ url(r'^index/xxxxx/xxx/xxx/xxx/xx/', views.index,name='inx'), ]
通过 usl名称,反向生成usl
include方法来实现路由转发功能
urls.py
url(r'^cmdb/', include'(crm.urls)'),
crm/urls.py
from django.conf.urls import url,include from django.contrib import admin from crm import views urlpatterns = [ url(r'^hosts/', views.hosts,name='hosts'), ]
crm/views.py
def hosts(request): return HttpResponse('主机列表')
通过这个cmdb前缀分发,分发到自己的urls中,好处可以将各种的业务拆分
反向生成时,使用reverse(‘namespace:name')
新建app, monitor 、openstack ,
url(r'^monitor/', include('monitor.urls',namespace='m')), url(r'^openstack/', include('openstack.urls',namespace='o')),
monitor/ulrs.py
from django.conf.urls import url,include from django.contrib import admin from monitor import views urlpatterns = [ url(r'^hosts/', views.hosts,name='hosts'), ]
monitor/views.py
from django.shortcuts import render,HttpResponse def hosts(request): return HttpResponse('监控系统,主机列表')
openstack /ulrs.py
from django.conf.urls import url,include from django.contrib import admin from openstack import views urlpatterns = [ url(r'^hosts/', views.hosts,name='hosts'), ]
openstack /views.py
from django.shortcuts import render,HttpResponse def hosts(request): return HttpResponse('OpenStack,主机列表')
登录成功时跳转到 openstack/主机列表页面
def login(request): return HttpResponse('Login') # 跳转到 openstack主机列表页面 # return redirect('/openstack/hosts/') url = reverse('o:hosts')
include本质 【就是元组,一级一级做分发】
urls.py
from monitor import views as mviews from openstack import views as oviews # /monitor/hosts/ url(r'^login/', views.login), url(r'^monitor/', ([ url(r'^hosts/',mviews.hosts,name='hhhhh'), url(r'^c1/',mviews.hosts), url(r'^x1/', ([ url(r'^xxx1/',mviews.hosts), url(r'^xxx2/',mviews.hosts,name='xx2'), url(r'^xxx3/',mviews.hosts), url(r'^xxx4/',mviews.hosts), ],None,None)), url(r'^c2/',mviews.hosts), url(r'^c3/',mviews.hosts), ],None,'mm')), url(r'^openstack/', ([ url(r'^hosts/',oviews.hosts,name='hhhhh'), url(r'^c1/',oviews.hosts), url(r'^c2/',oviews.hosts), url(r'^c3/',oviews.hosts), ],None,'oo')), ]
views.py
# 跳转到 openstack主机列表页面 url = reverse('oo:hhhhh') url = reverse('mm:uu:xx2')
动态生成url
views.py
def login(request): return HttpResponse('Login')
urls.py
urlpatterns = [ url(r'^login/', views.login), ] for i in range(10): temp = [] for j in range(5): temp.append(url(r'^inner_%s/' % j, views.login)) v = url(r'^login_%s/' %i, (temp,None,None)) urlpatterns.append(v)
以上就是Django中怎么反向生成url,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。