Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #21137 -- Documented best practice for URLconfs with repeated p…

…attern prefixes.
  • Loading branch information...
commit 222460a994072dfae9d5ac6b22a2afb116f64b2a 1 parent 5444a9c
@MichaelDiBernardo MichaelDiBernardo authored timgraham committed
Showing with 26 additions and 0 deletions.
  1. +26 −0 docs/topics/http/urls.txt
View
26 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

2 comments on commit 222460a

@marfire

Is this code correct? I haven't tested it, but glancing at django.conf.urls makes me think that prefixes aren't nested like this. That is, I don't think the 'wiki.views' prefix on the outer patterns gets used when the inner urls are resolved.

@mjtamlyn
Collaborator

Agreed - I've reopened the ticket.

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