Fixed #131 -- URLconfs that are 'included' now receive captured parameters 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
This commit is contained in:
Adrian Holovaty 2005-09-27 04:24:19 +00:00
parent 7f4067100c
commit c084005d58
2 changed files with 28 additions and 12 deletions

View File

@ -66,7 +66,7 @@ class RegexURLResolver(object):
tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0]['tried']]) tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0]['tried']])
else: else:
if sub_match: if sub_match:
return sub_match return sub_match[0], dict(match.groupdict(), **sub_match[1])
tried.append(pattern.regex.pattern) tried.append(pattern.regex.pattern)
raise Resolver404, {'tried': tried, 'path': new_path} raise Resolver404, {'tried': tried, 'path': new_path}

View File

@ -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'), (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 The first argument to ``patterns`` is an empty string in the above example, but
above example, but that argument is actually very useful. The first argument that argument can be useful. The first argument is prepended to all the view
will be prepended to all the view functions in the urlpatterns list, so the functions in the urlpatterns list, so the above example could be written more
above example could be written more concisely as:: concisely as::
urlpatterns = patterns('myproject.news.views.articles', urlpatterns = patterns('myproject.news.views.articles',
(r'^/articles/(?P<year>\d{4})/$', 'year_archive'), (r'^/articles/(?P<year>\d{4})/$', 'year_archive'),
@ -43,11 +43,11 @@ above example could be written more concisely as::
Including other URLconfs Including other URLconfs
======================== ========================
You can also "include" other URL config modules at any point along the path. You can also "include" other URLconf modules at any point along the path. This
This essentially "roots" a set of URLs below other ones. This is most often essentially "roots" a set of URLs below other ones. This is most often used
used for a site's "base" URLconfig; the ``ROOT_URLCONF`` setting points to a for a site's "base" URLconf; the ``ROOT_URLCONF`` setting points to a urlconf
urlconf module that will be used for the entire site. Here's the URLconf module that will be used for the entire site. Here's the URLconf for the
for the `Django website`_ itself. It includes a number of other URLconfs:: `Django website`_ itself. It includes a number of other URLconfs::
from django.conf.urls.defaults import * 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')), (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/ .. _`Django website`: http://www.djangoproject.com/
Passing extra options to view functions 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:: arguments that will be passed to the view function::
urlpatterns = patterns('myproject.news.views.articles', 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}),
) )