0%

Django路由系统

Django的路由是怎么回事在这里不再赘述,这里主要说一下include()的用法,以及装配路由的一点技巧。

include()

include主要有下面两种用法:

  • include(module, namespace=None)
  • include(pattern_list)

用法一:include(module, namespace=None)

这种用法中,参数module类型为str|object,它的值是一个模块,或者是一个模块路径,可以正常定位这个模块, include函数会自动加载module中的urlpatterns属性。 urlpatterns是一个列表,类型为list[RegexURLResolver|RegexURLPattern]

例如:

1
2
3
4
5
6
7
8
9
urlpatterns = [
# ... snip ...
url(r'^community/', include('django_website.aggregator.urls')),
url(r'^contact/', include('django_website.contact.urls')),
# ... snip ...
url(r'^author-polls/', include('polls.urls', namespace='author-polls')),
url(r'^publisher-polls/', include('polls.urls', namespace='publisher-polls')),
# ... snip ...
]

用法二:include(pattern_list)

参数pattern_list是一个列表,类型为list[RegexURLResolver|RegexURLPattern]

例如:

1
2
3
4
5
6
7
8
polls_patterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
]

urlpatterns = [
url(r'^polls/', include(polls_patterns)),
]

自动装配路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# ##################################
# #### <django_project>/urls.py ####
# ##################################
from importlib import import_module

from django.conf import settings
from django.conf.urls import url, include

urlpatterns = [] # settings.INSTALLED_APPS之外的urls,可直接在这写

_c_urlpatterns, _p_urlpatterns = [], [] # 具有相同url前缀,但又分属不同模块的pattern_list
for app in settings.INSTALLED_APPS:
if app.startswith('django.contrib'): # 忽略django内置模块
continue
try:
urlconf_module = import_module(app + '.urls')
urlpatterns += getattr(urlconf_module, 'urlpatterns', [])
_c_urlpatterns += getattr(urlconf_module, 'c_urlpatterns', [])
_p_urlpatterns += getattr(urlconf_module, 'p_urlpatterns', [])
except ImportError:
pass

urlpatterns += [
url(r'^cs/(?P<c_id>\w{1,32})', include(_c_urlpatterns)),
url(r'^ps/(?P<p_id>\w{1,32})', include(_p_urlpatterns)),
]