【问题标题】:Random URLs return error code 500 instead of 404随机 URL 返回错误代码 500 而不是 404
【发布时间】:2023-03-12 18:15:01
【问题描述】:

在我的urls.py 文件中,我有以下内容:

def error_404(request): return render(request, 'shop/errors/404.html')
def error_500(request): return render(request, 'shop/errors/500.html')

handler404 = error_404
handler500 = error_500

但是,当我输入随机网址时,例如“mysite.com/lhazeglazd”,会返回 500 错误页面。

是什么导致 500 错误覆盖我的 404 错误?

【问题讨论】:

    标签: django error-handling http-status-code-404


    【解决方案1】:

    您的日志或管理员错误电子邮件应说明 500 服务器错误的原因。

    请注意,在这种情况下,您不需要自定义 404 和 500 处理程序。默认情况下,默认处理程序会查找 404.html500.html,因此您只需将模板上移几个目录即可。

    如果您必须有一个自定义的 500 处理程序,请注意,不建议将请求传递给渲染 - 如果上下文处理器有问题,则在尝试渲染 500 模板时会再次发生错误。

    【讨论】:

    • 你是对的!我删除了处理程序并将我的404.html500.html 直接放在projectname/static/templates 中,现在一切正常!
    猜你喜欢
    • 2010-11-04
    • 2015-05-26
    • 2020-08-28
    • 2020-07-06
    • 2018-12-27
    • 2020-09-12
    • 2011-11-28
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多