Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #131 -- URLconfs that are 'included' now receive captured param…

…eters from parent URLconfs. Thanks for the idea, jcernelli@gmail.com

git-svn-id: http://code.djangoproject.com/svn/django/trunk@704 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c084005d5822baa6c4e6250df57ae4f5f7cd2876 1 parent 7f40671
@adrianholovaty adrianholovaty authored
Showing with 28 additions and 12 deletions.
  1. +1 −1  django/core/urlresolvers.py
  2. +27 −11 docs/url_dispatch.txt
View
2  django/core/urlresolvers.py
@@ -66,7 +66,7 @@ def resolve(self, path):
tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0]['tried']])
else:
if sub_match:
- return sub_match
+ return sub_match[0], dict(match.groupdict(), **sub_match[1])
tried.append(pattern.regex.pattern)
raise Resolver404, {'tried': tried, 'path': new_path}
View
38 docs/url_dispatch.txt
@@ -2,7 +2,7 @@
URL dispatcher
==============
-We're fanatics about good URLs. No ".php" or ".cgi", and certainly not any of
+We're fanatics about good URLs. No ".php" or ".cgi", and certainly not any of
that "0,2097,1-1-1928,00" nonsense. Django's URL dispatcher lets you design
your URLs to be as pretty as the rest of your application.
@@ -24,10 +24,10 @@ Here's the example from that overview::
(r'^/articles/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'myproject.news.views.articles.article_detail'),
)
-You can see that the first argument to ``patterns`` is an empty string in the
-above example, but that argument is actually very useful. The first argument
-will be prepended to all the view functions in the urlpatterns list, so the
-above example could be written more concisely as::
+The first argument to ``patterns`` is an empty string in the above example, but
+that argument can be useful. The first argument is prepended to all the view
+functions in the urlpatterns list, so the above example could be written more
+concisely as::
urlpatterns = patterns('myproject.news.views.articles',
(r'^/articles/(?P<year>\d{4})/$', 'year_archive'),
@@ -43,11 +43,11 @@ above example could be written more concisely as::
Including other URLconfs
========================
-You can also "include" other URL config modules at any point along the path.
-This essentially "roots" a set of URLs below other ones. This is most often
-used for a site's "base" URLconfig; the ``ROOT_URLCONF`` setting points to a
-urlconf module that will be used for the entire site. Here's the URLconf
-for the `Django website`_ itself. It includes a number of other URLconfs::
+You can also "include" other URLconf modules at any point along the path. This
+essentially "roots" a set of URLs below other ones. This is most often used
+for a site's "base" URLconf; the ``ROOT_URLCONF`` setting points to a urlconf
+module that will be used for the entire site. Here's the URLconf for the
+`Django website`_ itself. It includes a number of other URLconfs::
from django.conf.urls.defaults import *
@@ -59,6 +59,22 @@ for the `Django website`_ itself. It includes a number of other URLconfs::
(r'', include('django.conf.urls.flatfiles')),
)
+Note that an included URLconf receives any captured parameters from parent
+URLconfs, so the following example is valid::
+
+ # In settings/urls/main.py
+ urlpatterns = patterns('',
+ (r'^(?P<username>\w+)/blog/', include('foo.urls.blog')),
+ )
+
+ # In foo/urls/blog.py
+ urlpatterns = patterns('foo.views'
+ (r'^$', 'blog.index'),
+ (r'^archive/$', 'blog.archive'),
+
+In the above example, the captured ``"username"`` variable is passed to the
+included URLconf, as expected.
+
.. _`Django website`: http://www.djangoproject.com/
Passing extra options to view functions
@@ -70,5 +86,5 @@ in URLconf tuples. This third element can be a dictionary of extra keyword
arguments that will be passed to the view function::
urlpatterns = patterns('myproject.news.views.articles',
- (r'^/articles/(?P<year>\d{4})/$', 'year_archive', {key: value, key2: value 2}),
+ (r'^/articles/(?P<year>\d{4})/$', 'year_archive', {key: value, key2: value2}),
)
Please sign in to comment.
Something went wrong with that request. Please try again.