diff options
| author | Michael DiBernardo <mikedebo@gmail.com> | 2013-09-21 11:22:45 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-09-21 18:17:21 -0400 |
| commit | 222460a994072dfae9d5ac6b22a2afb116f64b2a (patch) | |
| tree | 1178c3dd086ddb9d6bb9a0804da9a2cff392c877 /docs | |
| parent | 5444a9c68353d8108df1cf69c9557740d626a18d (diff) | |
Fixed #21137 -- Documented best practice for URLconfs with repeated pattern prefixes.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/topics/http/urls.txt | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/docs/topics/http/urls.txt b/docs/topics/http/urls.txt index 83610e99c0..de3ef71fb7 100644 --- a/docs/topics/http/urls.txt +++ b/docs/topics/http/urls.txt @@ -365,6 +365,32 @@ instead. For example, consider this URLconf:: In this example, the ``/credit/reports/`` URL will be handled by the ``credit.views.report()`` Django view. +This can be used to remove redundancy from URLconfs where a single pattern +prefix is used repeatedly. For example, consider this URLconf:: + + from django.conf.urls import patterns, url + + urlpatterns = patterns('wiki.views', + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$', 'history'), + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$', 'edit'), + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$', 'discuss'), + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$', 'permissions'), + ) + +We can improve this by stating the common path prefix only once and grouping +the suffixes that differ:: + + from django.conf.urls import include, patterns, url + + urlpatterns = patterns('wiki.views', + url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/', include(patterns('', + url(r'^history/$', 'history'), + url(r'^edit/$', 'edit'), + url(r'^discuss/$', 'discuss'), + url(r'^permissions/$', 'permissions'), + ))), + ) + .. _`Django Web site`: https://www.djangoproject.com/ Captured parameters |
