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

I tried using https://docs.djangoproject.com/en/dev/topics/http/views/ , but still I get a standard 404 html page. I want to go to my custom view

handler404 = 'myview.views.custom_page_not_found' , 

I debugged it (using eclipse), then the value of handler404(old value -'django.config.default.views.page_not_found ) changed to the new value that I gave ('myview.views.custom_page_not_found'). But it still shows the top 404 page. And I changed settings.py DEBUG to False, after which I will display the user page. But it has some drawbacks (it will not load static files and that's it, DEBUG = false is the wrong way), so I had to reset to True.

Should I make some other modifications to implement this?

+6
source share
2 answers

I think that you cannot easily change the 404 page to DEBUG = True .

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 representation will never be used, and your URLconf with some debugging information will be displayed instead.

+2
source

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

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

Change 404.html to the appropriate template that you use, I believe that 404.html is the default. Then with debug = True you can check your 404 page.

If you want to test it with Debug = True, you need this at the bottom of the 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 starting with DEBUG = False don't forget to compile static:

 python manage.py collectstatic 

Hope this helps, Cheers!

0
source

Source: https://habr.com/ru/post/955185/


All Articles