Skip to content

Commit

Permalink
Fixed #878 -- URLconf regex captures no longer have to be named group…
Browse files Browse the repository at this point in the history
…s. Old URLconfs (with named groups) still work. This is backwards-incompatible if you've defined custom middleware with a process_view function. See http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1470 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Nov 27, 2005
1 parent 8c3b41c commit cc3660c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions django/core/handlers/base.py
Expand Up @@ -62,16 +62,16 @@ def get_response(self, path, request):

resolver = urlresolvers.RegexURLResolver(r'^/', ROOT_URLCONF)
try:
callback, param_dict = resolver.resolve(path)
callback, callback_args, callback_kwargs = resolver.resolve(path)

# Apply view middleware
for middleware_method in self._view_middleware:
response = middleware_method(request, callback, param_dict)
response = middleware_method(request, callback, callback_args, callback_kwargs)
if response:
return response

try:
response = callback(request, **param_dict)
response = callback(request, *callback_args, **callback_kwargs)
except Exception, e:
# If the view raised an exception, run it through exception
# middleware, and if the exception middleware returns a
Expand Down
20 changes: 15 additions & 5 deletions django/core/urlresolvers.py
Expand Up @@ -4,7 +4,7 @@
RegexURLResolver is the main class here. Its resolve() method takes a URL (as
a string) and returns a tuple in this format:
(view_function, dict_of_view_function_args)
(view_function, function_args, function_kwargs)
"""

from django.core.exceptions import Http404, ImproperlyConfigured, ViewDoesNotExist
Expand All @@ -31,12 +31,22 @@ def __init__(self, regex, callback, default_args=None):
def resolve(self, path):
match = self.regex.search(path)
if match:
args = dict(match.groupdict(), **self.default_args)
# If there are any named groups, use those as kwargs, ignoring
# non-named groups. Otherwise, pass all non-named arguments as
# positional arguments.
kwargs = match.groupdict()
if kwargs:
args = ()
if not kwargs:
args = match.groups()
# In both cases, pass any extra_kwargs as **kwargs.
kwargs.update(self.default_args)

try: # Lazily load self.func.
return self.func, args
return self.func, args, kwargs
except AttributeError:
self.func = self.get_callback()
return self.func, args
return self.func, args, kwargs

def get_callback(self):
mod_name, func_name = get_mod_func(self.callback)
Expand Down Expand Up @@ -66,7 +76,7 @@ def resolve(self, path):
tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0]['tried']])
else:
if sub_match:
return sub_match[0], dict(match.groupdict(), **sub_match[1])
return sub_match[0], sub_match[1], dict(match.groupdict(), **sub_match[2])
tried.append(pattern.regex.pattern)
raise Resolver404, {'tried': tried, 'path': new_path}

Expand Down
3 changes: 1 addition & 2 deletions django/middleware/doc.py
Expand Up @@ -5,8 +5,7 @@ class XViewMiddleware:
"""
Adds an X-View header to internal HEAD requests -- used by the documentation system.
"""

def process_view(self, request, view_func, param_dict):
def process_view(self, request, view_func, view_args, view_kwargs):
"""
If the request method is HEAD and the IP is internal, quickly return
with an x-header indicating the view function. This is used by the
Expand Down
2 changes: 1 addition & 1 deletion django/utils/decorators.py
Expand Up @@ -13,7 +13,7 @@ def _wrapped_view(request, *args, **kwargs):
if result is not None:
return result
if hasattr(middleware, 'process_view'):
result = middleware.process_view(request, view_func, **kwargs)
result = middleware.process_view(request, view_func, *args, **kwargs)
if result is not None:
return result
try:
Expand Down

0 comments on commit cc3660c

Please sign in to comment.