python - 如何覆盖 Django 中的标准 handler404、handler403、handler500?

标签 python django exception-handling

我试过使用 https://docs.djangoproject.com/en/dev/topics/http/views/教程,但我仍然得到标准的 404 html 页面。我想切换到我的自定义 View

handler404 = 'myview.views.custom_page_not_found' ,

我确实调试了它(使用 eclipse),然后 handler404(old value -'django.config.default.views.page_not_found) 的值被更改为我给定的新值 (' myview.views.custom_page_not_found')。但它仍然显示旧的 404 页面。我已经将 settings.py DEBUG 更改为 False 然后它显示自定义页面。但它有一些缺点(它不会加载静态文件和所有,DEBUG = false 不是正确的方法)所以我不得不重置为 True。

我是否必须进行一些其他修改才能实现它?

最佳答案

我认为您不能毫无困难地更改 DEBUG = True 模式下的 404 页面。

文档中有提示(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.

关于python - 如何覆盖 Django 中的标准 handler404、handler403、handler500?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19160164/

相关文章:

c++ - 错误处理 : return value vs exception in C++

c# - 什么会导致 throw 重置调用堆栈(我使用的是 "throw",而不是 "throw ex")

python - 如何将 defaultdict 中的每个计数相加?

python - ElasticBeanstalk Django : Please supply ENGINE value

validation - Grails,如果我发现错误,如何从域发送消息(类型 flash)?

django - 如何在 Django 表单中设置密码显示/隐藏眼睛按钮

python - 当 DEBUG=False 时服务器错误 500

打印行旁边的 Python 动画光标

python - 如何更改框架的背景 - Tkinter!?

python - 用于安装 Tensorflow 的虚拟环境 : Why Do I need it for Whiich Purpose?