The Python Oracle

How can I override standard handler404, handler403, handler500 in Django?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream

--

Chapters
00:00 How Can I Override Standard Handler404, Handler403, Handler500 In Django?
00:54 Accepted Answer Score 2
01:18 Answer 2 Score 0
02:03 Thank you

--

Full question
https://stackoverflow.com/questions/1916...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #exceptionhandling

#avk47



ACCEPTED ANSWER

Score 2


I think you can not change the 404 page in DEBUG = True mode without difficulty.

There is a hint in the documentation (https://docs.djangoproject.com/en/dev/topics/http/views/#the-404-page-not-found-view):

If DEBUG is set to True (in your settings module), then your 404 view will never be used, and your URLconf will be displayed instead, with some debug information.




ANSWER 2

Score 0


Try adding this to the bottom of your main urls.py:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^404/$', TemplateResponse, {'template': '404.html'}))

Swap out the 404.html to the appropriate template you use, I believe 404.html is the default though. Then with debug=True you can test out your 404 page.

If you want to Test it out with Debug=True then you need this at the bottom of your main urls.py instead:

#Enable static for runserver with debug false
from django.conf import settings
if settings.DEBUG is False:   #if DEBUG is True it will be served automatically
    urlpatterns += patterns('',
            url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

When running with DEBUG = False, don't forget to collect static:

python manage.py collectstatic

Hope this helps, Cheers!