Opened 10 years ago

Closed 10 years ago

#22756 closed New feature (fixed)

Technical 404 could say which view was run, if any.

Reported by: Keryn Knight <django@…> Owned by: jooyous
Component: Core (URLs) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given a view:

def myview(request):
    raise Http404('test')

which is mounted at, say, /a/b/c/, where a and b may be url namespaces themselves (ie: urlconfs pulled together via include('x.y')) it is laborious to actually establish the callable that was executed, without using django-debug-toolbar; you have to manually traverse from the ROOT_URLCONF to wherever the urlpatterns which mount the myview are (or use something like django-extensions show_urls; etc etc)

Coming on board to an existing project could be thus made easier, if, when a 404 is thrown by a view (rather than the absence of a view) information was expressed about where to look to alter or fix that.

In DEBUG, when the TECHNICAL_404_TEMPLATE is used, it ought to be possible to attempt to resolve the request.path to a named callable in many circumstances by adding a new value to the technical_404_response's Context, using something like the tests debug-toolbar does, which I reproduce here (changed for brevity/structure) in case they change over time:

name = _("<no view>")
try:
    obj = resolve(request.path)
except Resolver404:  # not what debug-toolbar catches; don't know why.
    pass
else:
    if hasattr(obj, '__name__'):
        name = obj.__name__
    elif hasattr(obj, '__class__') and hasattr(obj.__class__, '__name__'):
        name = obj.__class__.__name__

    if hasattr(obj, '__module__'):
        module = obj.__module__
        name = '%s.%s' % (module, name)

Change History (9)

comment:1 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted

comment:2 by jooyous, 10 years ago

Owner: changed from nobody to jooyous
Status: newassigned

comment:4 by jooyous, 10 years ago

Has patch: set

comment:5 by Tim Graham, 10 years ago

Patch needs improvement: set

I left comments for improvement on PR. Please uncheck "Patch needs improvement" when you update it, thanks.

comment:6 by jooyous, 10 years ago

Patch needs improvement: unset

I updated the patch, but I didn't add a test to make sure the view path is displayed instead of the view name. I tried adding one, but the view path was coming out automatically in the testing setup before I made this change.

comment:7 by Tim Graham, 10 years ago

Patch needs improvement: set

Looks good. I noted how you can update the test so that it will fail with the old version of the code and also suggested one more test so that all code paths are tested.

comment:8 by jooyous, 10 years ago

Patch needs improvement: unset

The test meant to fail on the old version still didn't work for me, so I left it out.

comment:9 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 29c1151a5577cccfdb18109ccb42330cde124d89:

Fixed #22756 -- Added view name to technical 404 template if Http404 is raised.

Thanks Keryn Knight for the suggestion.

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