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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3506 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-08-01 02:57:08 +00:00
parent 0f977e974c
commit 1a428ec9b8
4 changed files with 52 additions and 5 deletions

View File

@ -70,6 +70,7 @@ answer newbie questions, and generally made Django that much better:
Clint Ecker Clint Ecker
gandalf@owca.info gandalf@owca.info
Baishampayan Ghose Baishampayan Ghose
martin.glueck@gmail.com
Espen Grindhaug <http://grindhaug.org/> Espen Grindhaug <http://grindhaug.org/>
Brant Harris Brant Harris
hipertracker@gmail.com hipertracker@gmail.com

View File

@ -10,8 +10,10 @@ include = lambda urlconf_module: [urlconf_module]
def patterns(prefix, *tuples): def patterns(prefix, *tuples):
pattern_list = [] pattern_list = []
for t in tuples: for t in tuples:
if type(t[1]) == list: regex, view_or_include = t[:2]
pattern_list.append(RegexURLResolver(t[0], t[1][0])) default_kwargs = t[2:]
if type(view_or_include) == list:
pattern_list.append(RegexURLResolver(regex, view_or_include[0], *default_kwargs))
else: else:
pattern_list.append(RegexURLPattern(t[0], prefix and (prefix + '.' + t[1]) or t[1], *t[2:])) pattern_list.append(RegexURLPattern(regex, prefix and (prefix + '.' + view_or_include) or view_or_include, *default_kwargs))
return pattern_list return pattern_list

View File

@ -130,12 +130,13 @@ class RegexURLPattern(object):
return reverse_helper(self.regex, *args, **kwargs) return reverse_helper(self.regex, *args, **kwargs)
class RegexURLResolver(object): class RegexURLResolver(object):
def __init__(self, regex, urlconf_name): def __init__(self, regex, urlconf_name, default_kwargs=None):
# regex is a string representing a regular expression. # regex is a string representing a regular expression.
# urlconf_name is a string representing the module containing urlconfs. # urlconf_name is a string representing the module containing urlconfs.
self.regex = re.compile(regex) self.regex = re.compile(regex)
self.urlconf_name = urlconf_name self.urlconf_name = urlconf_name
self.callback = None self.callback = None
self.default_kwargs = default_kwargs or {}
def resolve(self, path): def resolve(self, path):
tried = [] tried = []
@ -149,7 +150,8 @@ 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[0], sub_match[1], dict(match.groupdict(), **sub_match[2]) sub_match_dict = dict(self.default_kwargs, **sub_match[2])
return sub_match[0], sub_match[1], dict(match.groupdict(), **sub_match_dict)
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

@ -389,3 +389,45 @@ to pass metadata and options to views.
.. _generic views: http://www.djangoproject.com/documentation/generic_views/ .. _generic views: http://www.djangoproject.com/documentation/generic_views/
.. _syndication framework: http://www.djangoproject.com/documentation/syndication/ .. _syndication framework: http://www.djangoproject.com/documentation/syndication/
Passing extra options to ``include()``
--------------------------------------
**New in the Django development version.**
Similarly, you can pass extra options to ``include()``. When you pass extra
options to ``include()``, *each* line in the included URLconf will be passed
the extra options.
For example, these two URLconf sets are functionally identical:
Set one::
# main.py
urlpatterns = patterns('',
(r'^blog/', include('inner'), {'blogid': 3}),
)
# inner.py
urlpatterns = patterns('',
(r'^archive/$', 'mysite.views.archive'),
(r'^about/$', 'mysite.views.about'),
)
Set two::
# main.py
urlpatterns = patterns('',
(r'^blog/', include('inner')),
)
# inner.py
urlpatterns = patterns('',
(r'^archive/$', 'mysite.views.archive', {'blogid': 3}),
(r'^about/$', 'mysite.views.about', {'blogid': 3}),
)
Note that extra options will *always* be passed to *every* line in the included
URLconf, regardless of whether the line's view actually accepts those options
as valid. For this reason, this technique is only useful if you're certain that
every view in the the included URLconf accepts the extra options you're passing.