命名空间
a. project.urls.py
1
2
3
4
5
6
|
from django.conf.urls import url,include urlpatterns = [ url(r '^a/' , include( 'app01.urls' , namespace = 'author-polls' )), url(r '^b/' , include( 'app01.urls' , namespace = 'publisher-polls' )), ] |
b. app01.urls.py
1
2
3
4
5
6
7
|
from django.conf.urls import url from app01 import views app_name = 'app01' urlpatterns = [ url(r '^(?P<pk>\d+)/$' , views.detail, name = 'detail' ) ] |
c. app01.views.py
1
2
3
|
def detail(request, pk): print (request.resolver_match) return HttpResponse(pk) |
以上定义带命名空间的url之后,使用name生成URL时候,应该如下:
- v = reverse('author-polls:detail', kwargs={'pk':11})
- {% url 'author-polls:detail' pk=12 pp=99 %}
django中的路由系统和其他语言的框架有所不同,在django中每一个请求的url都要有一条路由映射,这样才能将请求交给对一个的view中的函数去处理。其他大部分的Web框架则是对一类的url请求做一条路由映射,从而是路由系统变得简洁。