Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #21137 -- Documented best practice for URLconfs with re…

…peated pattern prefixes.

Backport of 222460a from master
  • Loading branch information...
commit 61b685847e63e94d678988d6c7a8a7477b37275d 1 parent b8e7730
Michael DiBernardo authored September 21, 2013 timgraham committed September 21, 2013

Showing 1 changed file with 26 additions and 0 deletions. Show diff stats Hide diff stats

  1. 26  docs/topics/http/urls.txt
26  docs/topics/http/urls.txt
@@ -366,6 +366,32 @@ instead. For example, consider this URLconf::
366 366
 In this example, the ``/credit/reports/`` URL will be handled by the
367 367
 ``credit.views.report()`` Django view.
368 368
 
  369
+This can be used to remove redundancy from URLconfs where a single pattern
  370
+prefix is used repeatedly. For example, consider this URLconf::
  371
+
  372
+    from django.conf.urls import patterns, url
  373
+
  374
+    urlpatterns = patterns('wiki.views',
  375
+        url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/history/$', 'history'),
  376
+        url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/edit/$', 'edit'),
  377
+        url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/discuss/$', 'discuss'),
  378
+        url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/permissions/$', 'permissions'),
  379
+    )
  380
+
  381
+We can improve this by stating the common path prefix only once and grouping
  382
+the suffixes that differ::
  383
+
  384
+    from django.conf.urls import include, patterns, url
  385
+
  386
+    urlpatterns = patterns('wiki.views',
  387
+        url(r'^(?P<page_slug>\w+)-(?P<page_id>\w+)/', include(patterns('',
  388
+            url(r'^history/$', 'history'),
  389
+            url(r'^edit/$', 'edit'),
  390
+            url(r'^discuss/$', 'discuss'),
  391
+            url(r'^permissions/$', 'permissions'),
  392
+        ))),
  393
+    )
  394
+
369 395
 .. _`Django Web site`: https://www.djangoproject.com/
370 396
 
371 397
 Captured parameters

0 notes on commit 61b6858

Please sign in to comment.
Something went wrong with that request. Please try again.