Opened 4 years ago

Closed 4 years ago

#25045 closed Bug (worksforme)

Django 1.8.2 handler404 and handler500 no longer work

Reported by: simon9999 Owned by: nobody
Component: Core (Other) Version: 1.8
Severity: Normal Keywords: handler404
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by simon9999)

I've been testing with DEBUG=False under 'python manage.py runserver'.
As per the dox, I have added into my ROOT url file, the required overrides:

from django.conf.urls import *
from django.contrib import admin
from django.http import HttpResponse

handler404 = 'urls.custom_handler'
handler500 = 'urls.custom_handler'

urlpatterns = [
    url(r'^apikey/', include('apikey.urls', namespace='apikey')),
    url(r'^admin/', include(admin.site.urls)),
]

def custom_handler(request, template_name='404.html'):
    print '---->>>> got to here!'
    raise Exception
    return HttpResponse('<h1>Boo! - Page not found</h1>')


I have created both the default 404.html and 500.html templates in my apikey/templates directory and updated the site settings file to reflect this
and I can confirm that this default 404.html and 500.html files are indeed getting rendered, but not by my routine.

My custom handler never, ever fires when a 404 or 500 is raised.

I am at a loss as to why this has stopped working and I have been searching both the tickets DB and the net for a solution so it may well be a bug.

Change History (2)

comment:1 Changed 4 years ago by simon9999

Description: modified (diff)

pasted in the wrong custom_handler() - now corrected

comment:2 Changed 4 years ago by Claude Paroz

Resolution: worksforme
Status: newclosed

I suspect this might be related to an old-school project structure. In old versions of Django, you could have importable files at the root of the project. This layout is no longer supported, and as you can see with running a startproject command with Django 1.8, the urls.py file is now inside a subdirectory of the project. Consequently, the handler path should have the subdirectory name as first component. If my diagnostic is wrong, then reopen the ticket and please attach a sample project structure that reproduces this problem.

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