关于django的url的namespace和view写法

2021-05-29 08:03

阅读:829

标签:结果   admin   div   redirect   def   code   direct   空间名   其他   

命名空间模式

  即使不同的APP使用相同的URL名称,URL的命名空间模式也可以让你唯一反转命名的URL。

  举个例子:

  项目的urls.py写法:

技术图片
from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r‘^app01/‘, include(‘app01.urls‘)),
    url(r‘^app02/‘, include(‘app02.urls‘)),

]
技术图片

  app01下的urls.py写法

技术图片
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    # url(r‘^admin/‘, admin.site.urls),
    url(r‘^index/‘, views.index,name=‘index‘),

]
技术图片

  app02下的urls.py的写法

技术图片
from django.conf.urls import url
from django.contrib import admin
from app02 import views
# app_name = ‘app02‘
urlpatterns = [
    # url(r‘^admin/‘, admin.site.urls),
    url(r‘^index/‘, views.index,name=‘index‘),
]
技术图片

  app01下的views.py的写法

技术图片
from django.shortcuts import render,HttpResponse,redirect
from django.urls import reverse
# Create your views here.

def index(request):
    print(reverse(‘index‘))
    return HttpResponse(‘ok‘)
技术图片

  app02下的views.py的写法

技术图片
from django.shortcuts import render,HttpResponse,redirect
from django.urls import reverse
# Create your views here.
def index(request):

    print(reverse(‘index‘))
    return HttpResponse(‘ok2‘)
技术图片

  你会发现,不管你是访问app01下的index还是app02下的index,打印的结果都是/app02/index/,也就是打印的是最后一个index别名对应的url路径。所以别名冲突了的话就需要我们的命名空间来保证别名对应的url的唯一性了。

  project中的urls.py

from django.conf.urls import url, include
 
urlpatterns = [
    url(r‘^app01/‘, include(‘app01.urls‘, namespace=‘app01‘)),
    url(r‘^app02/‘, include(‘app02.urls‘, namespace=‘app02‘)),
]

 

  app01中的urls.py

from django.conf.urls import url
from app01 import views
 
urlpatterns = [
    url(r‘^(?P\d+)/$‘, views.detail, name=‘detail‘)
]

 

  app02中的urls.py

from django.conf.urls import url
from app02 import views
 
urlpatterns = [
    url(r‘^(?P\d+)/$‘, views.detail, name=‘detail‘)
]

 

  现在,我的两个app中 url名称重复了,我反转URL的时候就可以通过命名空间的名称得到我当前的URL。

  语法:

  ‘命名空间名称:URL名称‘

  模板中使用:

{% url ‘app01:detail‘ pk=12 pp=99 %}

  views中的函数中使用

v = reverse(‘app01:detail‘, kwargs={‘pk‘:11})

   这样即使app中URL的命名相同,我也可以反转得到正确的URL了。

 

第二种写法:就是在每个app下的urls.py文件中指定app名称,同样是命名空间。

技术图片
from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    # url(r‘^app01/‘, include(‘app01.urls‘,namespace=‘app01‘)),
    url(r‘^app01/‘, include(‘app01.urls‘)),
    # url(r‘^app02/‘, include(‘app02.urls‘,namespace=‘app02‘)),
    url(r‘^app02/‘, include(‘app02.urls‘)),

]
技术图片

 

app01下的urls.py

技术图片
from django.conf.urls import url
from django.contrib import admin
from app01 import views
app_name = ‘app01‘
urlpatterns = [
    # url(r‘^admin/‘, admin.site.urls),
    url(r‘^index/‘, views.index,name=‘index‘),

]
技术图片

 

app02下的urls.py

技术图片
from django.conf.urls import url
from django.contrib import admin
from app02 import views
app_name = ‘app02‘
urlpatterns = [
    # url(r‘^admin/‘, admin.site.urls),
    url(r‘^index/‘, views.index,name=‘index‘),
]
技术图片

 

 app01下的view视图中反向解析

def index(request):
    print(reverse(‘app01:index‘))
    return HttpResponse(‘ok‘)

 

  app02下的view视图中反向解析

def index(request):

    print(reverse(‘app02:index‘))
    return HttpResponse(‘ok2‘)

 

 

第三种写法:

项目的urls.py中的写法:

技术图片
from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    # url(r‘^app01/‘, include(‘app01.urls‘,namespace=‘app01‘)),
    url(r‘^app01/‘, include((‘app01.urls‘,‘app01‘),namespace=‘app01‘)),
    # url(r‘^app02/‘, include(‘app02.urls‘,namespace=‘app02‘)),
    url(r‘^app02/‘, include((‘app02.urls‘,‘app02‘),namespace=‘app02‘)),

]
技术图片

 

那么其他的app中的urls.py中就不需要写app_name=‘应用名‘了,看app01下urls.py的写法:

技术图片
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    # url(r‘^admin/‘, admin.site.urls),
    url(r‘^index/‘, views.index,name=‘index‘),

]
技术图片

 

 app02下urls.py的写法:

技术图片
from django.conf.urls import url
from django.contrib import admin
from app02 import views

urlpatterns = [
    # url(r‘^admin/‘, admin.site.urls),
    url(r‘^index/‘, views.index,name=‘index‘),
]
技术图片

 

视图的写法:

技术图片
#app01
def index(request):
    print(reverse(‘app01:index‘))
    return HttpResponse(‘ok‘)

#app02
def index(request):
    print(reverse(‘app02:index‘))
    return HttpResponse(‘ok2‘)

关于django的url的namespace和view写法

标签:结果   admin   div   redirect   def   code   direct   空间名   其他   

原文地址:https://www.cnblogs.com/zzj666/p/14747279.html


评论


亲,登录后才可以留言!