反向生成url 一.常規的url系統: from django.conf.urls import url from django.shortcuts import HttpResponse, render, redirect def index(request): return HttpRespon ...
反向生成url
一.常規的url系統:
from django.conf.urls import url
from django.shortcuts import HttpResponse, render, redirect
def index(request):
return HttpResponse("進入index")
def text(requext):
return HttpResponse("進入text")
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^index/', index),
url(r'^text/', text),
]
執行過程如下圖:
從這裡我們知道常規的url是需要在地址欄中將訪問的地址對應著urlpatterns中的地址吻合進行訪問,所以當出現下麵這種情況的話就對訪問的友好度極低:
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^arya/', site.urls),
url(r'^index/asdas/asdas/da/asd/as', index),
url(r'^text/dasd/asd/asd/sad/asd/as/d', text),
]
二、反向生成url
1.reverse方法
def index(request):
from django.urls import reverse
test_url = reverse('xx')
print(test_url)
return HttpResponse("進入index")
def text(requext):
return HttpResponse("進入text")
urlpatterns = [
url(r'^index/', index,),
url(r'^text/dasd/asd/asd/sad/asd/as/d', text,name='xx'),
]
列印的結果:
/text/dasd/asd/asd/sad/asd/as/d
這個url就是text的url路徑,所以通過給其定義一個name就可以反向生成一個其對應的url地址
2.帶參數的url的反向生成:
def index(request):
from django.urls import reverse
test_url = reverse('xx',args=(2,9))
return redirect(test_url)
def text(requext,*args,**kwargs):
return HttpResponse("進入text")
urlpatterns = [
url(r'^index/', index,),
url(r'^text/dasd/asd/(\d+)/sad/(\d+)/as/d', text,name='xx'),
]
這裡需要註意的是當text中的url中需要傳參所以對應的text視圖函數中就需要*args,**kwargs接受參數
3.帶命名的正則url
def index(request):
from django.urls import reverse
test_url = reverse('xx',kwargs={'a1':8,'a2':99})
return redirect(test_url)
def text(requext,*args,**kwargs):
return HttpResponse("進入text")
urlpatterns = [
url(r'^index/', index,),
url(r'^text/dasd/asd/(?P<a1>\d+)/sad/(?P<a2>\d+)/as/d', text,name='xx'),
]
這裡和常規帶參數的唯一不同reverse方法中是傳遞一個kwargs的鍵值對的字典
4.前端顯示反向url
- test_url = reverse('xx',kwargs={'a1':8,'a2':99})
渲染效果:
- test_url = reverse('xx',args=(2,9))
渲染效果:
反向生成url嵌套(namespance)
1.單層嵌套
def index(request):
from django.urls import reverse
test_url = reverse('x1:txt')
return HttpResponse("index")
def text(request,*args,**kwargs):
return HttpResponse('text')
urlpatterns = [
url(r'^app01/', ([
url(r'^index/', index,name='idx'),
url(r'^test/', text,name='txt')
],'x1','x2'),),
]
上述的url中相當於嵌套了一層,且有namespance(x1,x2),所以對於這類的url的反向生成需要如下操作:
test_url = reverse('x1:txt')
2.多層嵌套
def index(request):
from django.urls import reverse
test_url = reverse('x1:txt')
return HttpResponse("index")
def text(request,*args,**kwargs):
return HttpResponse('text')
urlpatterns = [
url(r'^app01/', ([
url(r'^userinfo/', (
[
url(r'^index/', index,name='idx'),
url(r'^test/', text,name='txt')
],None,None)),
] ,'x1','x2')),
]
這裡雙層嵌套的url,但是內層的namespance都是空所以它會往外層找,所
以反向生成的代碼:
test_url = reverse('x1:txt')
同時當內層namespance有值時:
urlpatterns = [
# url(r'^admin/', admin.site.urls),
# url(r'^arya/', site.urls),
url(r'^app01/', ([
url(r'^userinfo/', (
[
url(r'^index/', index,name='idx'),
url(r'^test/', text,name='txt')
],'x2','x2')),
] ,'x1','x1')),
]
反向生成url代碼就是如下:
def index(request):
from django.urls import reverse
test_url = reverse('x1:x2:txt')
print(test_url)
return HttpResponse("index")
def text(request,*args,**kwargs):
return HttpResponse('text')
所以通過上述的案例可以知道,不管url嵌套多少層,其反向生成url的規則是一樣的,遵循的規則就是namespance從外層以冒號連接至最後一層。