[英]Django3, custom 404 page redirects to 500 when Debug=False because Django cannot find exception parameter
When Debug = True
django returns a Page not found (404)
page which is totally fine当Debug = True
django 返回一个Page not found (404)
页面,这完全没问题
But when I change Debug = False
django return a Server Error (500)
and the server shows these requests made:但是当我更改Debug = False
django 返回Server Error (500)
并且服务器显示这些请求时:
[30/Jul/2022 12:29:44] "GET /__reload__/events/ HTTP/1.1" 500 145
[30/Jul/2022 12:29:44] "GET /__reload__/events/ HTTP/1.1" 500 145
[30/Jul/2022 12:29:44] "GET /sdfs HTTP/1.1" 500 145
[30/Jul/2022 12:29:45] "GET /favicon.ico HTTP/1.1" 302 0
myproject main urls.py
我的项目主urls.py
from django.conf.urls import handler404
handler404 = 'myproject.views.custom_page_not_found'
please note here that hander404 is not found by django handler404 not found请注意此处未找到hander404 django handler404 not found
my views.py
我的views.py
def custom_page_not_found(request, exception, template_name="errors/404.html"):
return render(request, template_name, status=404)
Please not here django cannot find the exception
parameter exception parameter not found请不要在这里 django 找不到exception
参数exception parameter not found
In previous community questions i could not find any question that points to the exception
parameter not found issue.在以前的社区问题中,我找不到任何指向exception
参数未找到问题的问题。 Please note that i am not using django templates {% %}
in my 404.html
file请注意,我没有在我的404.html
文件中使用 django 模板{% %}
I think when django does not find the exception parameter it returns server error 500.我认为当 django 没有找到异常参数时,它会返回服务器错误 500。
you don't need to import this handler from django.conf.urls import handler404
.您不需要from django.conf.urls import handler404
导入此处理程序。
you should simply to define variable in url:您应该简单地在 url 中定义变量:
handler404 = 'myproject.views.custom_page_not_found'
as function of view (is better) you can made this:作为 function 的视图(更好),您可以这样做:
def custom_page_not_found(request, *args, template_name="errors/404.html", **kargs):
return render(request, template_name, status=404)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.