您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。