1
0
mirror of https://github.com/django/django.git synced 2025-10-25 06:36:07 +00:00

Fixed #18373 - improved handling of Resolver404s from views

When django.core.urlresolvers.resolve was called from a view, failed
and the exception was propagated and rendered by technical_404_response,
the URL mentioned on the page was the current URL instead of the URL
passed to resolve().

Fixed by using the path attribute from the Resolver404 exception instead
of request.path_info. Also cleaned up the exceptions to use standard
named parameters instead of stuffing a dict in args[0]
This commit is contained in:
Grzegorz Nosek
2014-02-15 14:41:01 +01:00
committed by Honza Král
parent 8bbdcc76e4
commit 79558c787e
5 changed files with 25 additions and 12 deletions

View File

@@ -69,8 +69,11 @@ class ResolverMatch(object):
class Resolver404(Http404):
pass
def __init__(self, path, tried=None):
super(Resolver404, self).__init__()
self.path = path
self.tried = tried
class NoReverseMatch(Exception):
pass
@@ -322,7 +325,7 @@ class RegexURLResolver(LocaleRegexProvider):
try:
sub_match = pattern.resolve(new_path)
except Resolver404 as e:
sub_tried = e.args[0].get('tried')
sub_tried = e.tried
if sub_tried is not None:
tried.extend([pattern] + t for t in sub_tried)
else:
@@ -333,8 +336,8 @@ class RegexURLResolver(LocaleRegexProvider):
sub_match_dict.update(sub_match.kwargs)
return ResolverMatch(sub_match.func, sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces)
tried.append([pattern])
raise Resolver404({'tried': tried, 'path': new_path})
raise Resolver404({'path': path})
raise Resolver404(new_path, tried=tried)
raise Resolver404(path)
@property
def urlconf_module(self):

View File

@@ -475,9 +475,11 @@ class ExceptionReporter(object):
def technical_404_response(request, exception):
"Create a technical 404 error response. The exception should be the Http404."
try:
tried = exception.args[0]['tried']
except (IndexError, TypeError, KeyError):
tried = exception.tried
error_url = exception.path
except AttributeError:
tried = []
error_url = request.path_info[1:] # Trim leading slash
else:
if (not tried # empty URLconf
or (request.path == '/'
@@ -494,7 +496,7 @@ def technical_404_response(request, exception):
c = Context({
'urlconf': urlconf,
'root_urlconf': settings.ROOT_URLCONF,
'request_path': request.path_info[1:], # Trim leading slash
'request_path': error_url,
'urlpatterns': tried,
'reason': force_bytes(exception, errors='replace'),
'request': request,