Django 反向生成url实例详解

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实例详解 的全部内容, 来源链接: utcz.com/z/312506.html

回到顶部