简体   繁体   中英

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

I have tried using https://docs.djangoproject.com/en/dev/topics/http/views/ tutorial, but still I am getting the standard 404 html page. I want to switch in to my custom view

handler404 = 'myview.views.custom_page_not_found' ,

I did debug it (using eclipse), then the value of handler404(old value -'django.config.default.views.page_not_found ) is changed to the new value I have given ('myview.views.custom_page_not_found'). But it's still showing the older 404 page. And I have changed settings.py DEBUG into False then it shows the custom page. But it got some disadvantages (it won't load static files and all, DEBUG = false is not the right way) so I had to reset to True.

Do I have to make some other modification for implementing this?

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM