Django

Code

Changeset 3506

Show
Ignore:
Timestamp:
07/31/06 21:57:08 (2 years ago)
Author:
adrian
Message:

Fixed #2370 -- It's now possible to pass default URLconf arguments to include(). Added docs, as well. Thanks for the patch, martin.glueck@gmail.com

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r3477 r3506  
    7171    gandalf@owca.info 
    7272    Baishampayan Ghose 
     73    martin.glueck@gmail.com 
    7374    Espen Grindhaug <http://grindhaug.org/> 
    7475    Brant Harris 
  • django/trunk/django/conf/urls/defaults.py

    r411 r3506  
    1111    pattern_list = [] 
    1212    for t in tuples: 
    13         if type(t[1]) == list: 
    14             pattern_list.append(RegexURLResolver(t[0], t[1][0])) 
     13        regex, view_or_include = t[:2] 
     14        default_kwargs = t[2:] 
     15        if type(view_or_include) == list: 
     16            pattern_list.append(RegexURLResolver(regex, view_or_include[0], *default_kwargs)) 
    1517        else: 
    16             pattern_list.append(RegexURLPattern(t[0], prefix and (prefix + '.' + t[1]) or t[1], *t[2:])) 
     18            pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include) or view_or_include, *default_kwargs)) 
    1719    return pattern_list 
  • django/trunk/django/core/urlresolvers.py

    r3470 r3506  
    131131 
    132132class RegexURLResolver(object): 
    133     def __init__(self, regex, urlconf_name): 
     133    def __init__(self, regex, urlconf_name, default_kwargs=None): 
    134134        # regex is a string representing a regular expression. 
    135135        # urlconf_name is a string representing the module containing urlconfs. 
     
    137137        self.urlconf_name = urlconf_name 
    138138        self.callback = None 
     139        self.default_kwargs = default_kwargs or {} 
    139140 
    140141    def resolve(self, path): 
     
    150151                else: 
    151152                    if sub_match: 
    152                         return sub_match[0], sub_match[1], dict(match.groupdict(), **sub_match[2]) 
     153                        sub_match_dict = dict(self.default_kwargs, **sub_match[2]) 
     154                        return sub_match[0], sub_match[1], dict(match.groupdict(), **sub_match_dict) 
    153155                    tried.append(pattern.regex.pattern) 
    154156            raise Resolver404, {'tried': tried, 'path': new_path} 
  • django/trunk/docs/url_dispatch.txt

    r3505 r3506  
    390390.. _generic views: http://www.djangoproject.com/documentation/generic_views/ 
    391391.. _syndication framework: http://www.djangoproject.com/documentation/syndication/ 
     392 
     393Passing extra options to ``include()`` 
     394-------------------------------------- 
     395 
     396**New in the Django development version.** 
     397 
     398Similarly, you can pass extra options to ``include()``. When you pass extra 
     399options to ``include()``, *each* line in the included URLconf will be passed 
     400the extra options. 
     401 
     402For example, these two URLconf sets are functionally identical: 
     403 
     404Set one:: 
     405 
     406    # main.py 
     407    urlpatterns = patterns('', 
     408        (r'^blog/', include('inner'), {'blogid': 3}), 
     409    ) 
     410 
     411    # inner.py 
     412    urlpatterns = patterns('', 
     413        (r'^archive/$', 'mysite.views.archive'), 
     414        (r'^about/$', 'mysite.views.about'), 
     415    ) 
     416 
     417Set two:: 
     418 
     419    # main.py 
     420    urlpatterns = patterns('', 
     421        (r'^blog/', include('inner')), 
     422    ) 
     423 
     424    # inner.py 
     425    urlpatterns = patterns('', 
     426        (r'^archive/$', 'mysite.views.archive', {'blogid': 3}), 
     427        (r'^about/$', 'mysite.views.about', {'blogid': 3}), 
     428    ) 
     429 
     430Note that extra options will *always* be passed to *every* line in the included 
     431URLconf, regardless of whether the line's view actually accepts those options 
     432as valid. For this reason, this technique is only useful if you're certain that 
     433every view in the the included URLconf accepts the extra options you're passing.