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']])
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 File

@ -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}),
)