Django 反向生成url实例详解

时间:2021-05-22

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,HttpResponsedef index(request): return HttpResponse('Index')def login(request): return HttpResponse('longin')

urls.py

from django.conf.urls import urlfrom app01 import viewsurlpatterns = [ url(r'^index/', views.index), url(r'^login/', views.login),]

在平常做法

views.py

views.pydef 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 reversedef 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,includefrom django.contrib import adminfrom crm import viewsurlpatterns = [ 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,includefrom django.contrib import adminfrom monitor import viewsurlpatterns = [ url(r'^hosts/', views.hosts,name='hosts'),]

monitor/views.py

from django.shortcuts import render,HttpResponsedef hosts(request): return HttpResponse('监控系统,主机列表')

openstack /ulrs.py

from django.conf.urls import url,includefrom django.contrib import adminfrom openstack import viewsurlpatterns = [ url(r'^hosts/', views.hosts,name='hosts'),]

openstack /views.py

from django.shortcuts import render,HttpResponsedef 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 mviewsfrom 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)

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章