Django

Code

Changeset 704

Show
Ignore:
Timestamp:
09/26/05 23:24:19 (3 years ago)
Author:
adrian
Message:

Fixed #131 -- URLconfs that are 'included' now receive captured parameters from parent URLconfs. Thanks for the idea, jcernelli@gmail.com

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/urlresolvers.py

    r479 r704  
    6767                else: 
    6868                    if sub_match: 
    69                         return sub_match 
     69                        return sub_match[0], dict(match.groupdict(), **sub_match[1]) 
    7070                    tried.append(pattern.regex.pattern) 
    7171            raise Resolver404, {'tried': tried, 'path': new_path} 
  • django/trunk/docs/url_dispatch.txt

    r286 r704  
    33============== 
    44 
    5 We're fanatics about good URLs. No ".php" or ".cgi", and certainly not any of 
     5We're fanatics about good URLs. No ".php" or ".cgi", and certainly not any of 
    66that "0,2097,1-1-1928,00" nonsense. Django's URL dispatcher lets you design 
    77your URLs to be as pretty as the rest of your application. 
     
    2525    ) 
    2626 
    27 You can see that the first argument to ``patterns`` is an empty string in the 
    28 above example, but that argument is actually very useful. The first argument 
    29 will be prepended to all the view functions in the urlpatterns list, so th
    30 above example could be written more concisely as:: 
     27The first argument to ``patterns`` is an empty string in the above example, but 
     28that argument can be useful. The first argument is prepended to all the view 
     29functions in the urlpatterns list, so the above example could be written mor
     30concisely as:: 
    3131 
    3232    urlpatterns = patterns('myproject.news.views.articles', 
     
    4444======================== 
    4545 
    46 You can also "include" other URL config modules at any point along the path. 
    47 This essentially "roots" a set of URLs below other ones.  This is most often 
    48 used for a site's "base" URLconfig; the ``ROOT_URLCONF`` setting points to a 
    49 urlconf module that will be used for the entire site. Here's the URLconf 
    50 for the `Django website`_ itself. It includes a number of other URLconfs:: 
     46You can also "include" other URLconf modules at any point along the path. This 
     47essentially "roots" a set of URLs below other ones.  This is most often used 
     48for a site's "base" URLconf; the ``ROOT_URLCONF`` setting points to a urlconf 
     49module that will be used for the entire site. Here's the URLconf for the 
     50`Django website`_ itself. It includes a number of other URLconfs:: 
    5151 
    5252    from django.conf.urls.defaults import * 
     
    5959        (r'',                include('django.conf.urls.flatfiles')), 
    6060    ) 
     61 
     62Note that an included URLconf receives any captured parameters from parent 
     63URLconfs, so the following example is valid:: 
     64 
     65    # In settings/urls/main.py 
     66    urlpatterns = patterns('', 
     67        (r'^(?P<username>\w+)/blog/', include('foo.urls.blog')), 
     68    ) 
     69 
     70    # In foo/urls/blog.py 
     71    urlpatterns = patterns('foo.views' 
     72        (r'^$', 'blog.index'), 
     73        (r'^archive/$', 'blog.archive'), 
     74 
     75In the above example, the captured ``"username"`` variable is passed to the 
     76included URLconf, as expected. 
    6177 
    6278.. _`Django website`: http://www.djangoproject.com/ 
     
    7187 
    7288    urlpatterns = patterns('myproject.news.views.articles', 
    73         (r'^/articles/(?P<year>\d{4})/$', 'year_archive', {key: value, key2: value 2}), 
     89        (r'^/articles/(?P<year>\d{4})/$', 'year_archive', {key: value, key2: value2}), 
    7490    )