欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

反向生成url

程序员文章站 2022-03-03 10:42:05
反向生成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

从这里我们知道常规的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

二、反向生成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})

反向生成url

渲染效果:

反向生成url

test_url = reverse('xx',args=(2,9))

反向生成url

渲染效果:

反向生成url

反向生成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从外层以冒号连接至最后一层。