summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael DiBernardo <mikedebo@gmail.com>2013-09-21 11:22:45 -0400
committerTim Graham <timograham@gmail.com>2013-09-21 18:18:26 -0400
commit61b685847e63e94d678988d6c7a8a7477b37275d (patch)
treed930c3b8f12937dc418ed94dd0e49236627d3659
parentb8e7730f3e25a0bcabca82bf5a2e06411ffcc92e (diff)
[1.5.x] Fixed #21137 -- Documented best practice for URLconfs with repeated pattern prefixes.
Backport of 222460a994 from master
-rw-r--r--docs/topics/http/urls.txt26
1 files changed, 26 insertions, 0 deletions
diff --git a/docs/topics/http/urls.txt b/docs/topics/http/urls.txt
index cfe1f4619e..fc8abfaaef 100644
--- a/docs/topics/http/urls.txt
+++ b/docs/topics/http/urls.txt
@@ -366,6 +366,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