The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC H Dvoks String Quartet No 12 Ame

--

Chapters
00:00 Question
01:12 Accepted answer (Score 2)
01:39 Answer 2 (Score 0)
02:39 Thank you

--

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

Question links:
https://docs.djangoproject.com/en/dev/to.../

Accepted answer links:
https://docs.djangoproject.com/en/dev/to...

--

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!