Opened 9 years ago

Closed 8 years ago

Last modified 7 years ago

#12083 closed (fixed)

technical_404_response should handle KeyError because 'tried' key might be not present

Reported by: Maciej Wiśniowski Owned by: nobody
Component: Core (Other) Version: 1.1
Severity: Keywords:
Cc: pigletto@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


django/>technical_404_response expects key 'tried' to exist in args[0] dictionary:

def technical_404_response(request, exception):
    "Create a technical 404 error response. The exception should be the Http404."
        tried = exception.args[0]['tried']
    except (IndexError, TypeError):
        tried = []

Above is not always true. For example: django/core/>RegexURLResolver.resolve can raise Resolver404 exception without 'tried' key.

                raise Resolver404, {'tried': tried, 'path': new_path}
        raise Resolver404, {'path' : path}

Possible solution is to add KeyError to list of exceptions in technical_404_response.

Attachments (1)

patch_12083.diff (2.4 KB) - added by Maciej Wiśniowski 9 years ago.
proposed patch

Download all attachments as: .zip

Change History (7)

comment:1 Changed 9 years ago by Russell Keith-Magee

Resolution: worksforme
Status: newclosed

As a matter of principle, I'm marking this worksforme.

The most important part of a bug report is *the instructions for how to reproduce the bug*. I have no difficulty believing that you have seen this error. However, I am not going to apply a fix without an understanding of the underlying problem.

If you care to provide instructions on how to reproduce your problem (or better yet, a programatic test case for Django's test suite), feel free to reopen.

Changed 9 years ago by Maciej Wiśniowski

Attachment: patch_12083.diff added

proposed patch

comment:2 Changed 9 years ago by Maciej Wiśniowski

Has patch: set
Resolution: worksforme
Status: closedreopened

I've attached diff file with test case and patch included.

Failing test gives output like:

ERROR: test_404 (regressiontests.views.tests.debug.DebugViewTests)
Traceback (most recent call last):
  File "/home/svnSandbox/django_dev/tests/regressiontests/views/tests/", line 24, in test_404
    response = self.client.get('/views/raises404/')
  File "/home/svnSandbox/django_dev/django/test/", line 281, in get
    response = self.request(**r)
  File "/home/svnSandbox/django_dev/django/core/handlers/", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/svnSandbox/django_dev/tests/regressiontests/views/", line 46, in raises404
    return technical_404_response(request, e)
  File "/home/svnSandbox/django_dev/django/views/", line 247, in technical_404_response
    tried = exception.args[0]['tried']
KeyError: 'tried'

The problem may appear only when request.path_info doesn't match regex r'/' defined for RegexURLResolver (

    def get_resolver(urlconf):
        if urlconf is None:
            from django.conf import settings
            urlconf = settings.ROOT_URLCONF
        return RegexURLResolver(r'^/', urlconf)

    class RegexURLResolver(object):
        def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None):
            # regex is a string representing a regular expression.
            # urlconf_name is a string representing the module containing URLconfs.
            self.regex = re.compile(regex, re.UNICODE)

        def resolve(self, path):
            tried = []
            match =
            if match:
                raise Resolver404, {'tried': tried, 'path': new_path}
            raise Resolver404, {'path' : path}

I've found this issue when doing some (possibly dirty) hacking on request.path_info in my middleware class.
Otherwise it seems to be not possible to raise this exception as both WSGIRequest and ModPythonRequest are forcing request.path_info to be '/' instead of empty string. However, RegexURLResolver.resolve reflects this situation so provided patch might be desirable.

comment:3 Changed 8 years ago by Russell Keith-Magee

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:4 Changed 8 years ago by Karen Tracey

Resolution: fixed
Status: reopenedclosed

(In [12679]) Fixed #12083: Ensured generating debug 404 page won't raise a key error. Thanks pigletto.

comment:5 Changed 8 years ago by Karen Tracey

(In [12680]) [1.1.X] Fixed #12083: Ensured generating debug 404 page won't raise a key error. Thanks pigletto.

r12679 from trunk.

comment:6 Changed 7 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top