为了完成上面例子中的URL 反查,你将需要使用命名的URL 模式。URL 的名称使用的字符串可以包含任何你喜欢的字符。不只限制在合法的Python 名称。
当命名你的URL 模式时,请确保使用的名称不会与其它应用中名称冲突。如果你的URL 模式叫做comment,而另外一个应用中也有一个同样的名称,当你在模板中使用这个名称的时候不能保证将插入哪个URL。
在URL 名称中加上一个前缀,比如应用的名称,将减少冲突的可能。我们建议使用myapp-comment 而不是comment。
五 命名空间模式这个内容,我们后面用到的时候再过来学吧,比较靠后的一个内容~~~
即使不同的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写法
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<pk>\d+)/$', views.detail,) ]app02中的urls.py
from django.conf.urls import url from app02 import views urlpatterns = [ url(r'^(?P<pk>\d+)/$', views.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')), ]