【问题标题】:handler404 returning Server Error (500) instead of 404.html - Django/Pythonhandler404 返回服务器错误(500)而不是 404.html - Django/Python
【发布时间】:2020-09-12 14:16:27
【问题描述】:

我在这段代码上花了几个小时,但无法找出为什么我在 django 程序中收到服务器错误 (500) 而不是 404.html。请看下面的代码,有人可以帮忙吗?

settings.py: 
DEBUG = False
ALLOWED_HOSTS = ['*']

urls.py: (app is mysite)
handler404 = "mysite.views.error_404"

views.py: 
def error_404(request, exception):
    return render(request, '404.html')
404.html:
{% extends "base.html" %}
{% load static %}
{% block main %}

some html text here

{% endblock %}

来自日志的错误消息:

【问题讨论】:

  • 你的项目中确定有一个叫mysite的应用吗?
  • 另外,你能从运行服务器添加错误回溯吗?
  • 是的,我的应用名为 mysite。
  • 你能解决这个问题吗?
  • 我有同样的错误...

标签: python django


【解决方案1】:

尝试摆脱 404 页面中的所有扩展命令,并呈现一个没有 Django 扩展的纯 HTML 页面。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

可能是404文件的路径:

def error_404(request, exception):
    return render(request, 'mysite/404.html') # Instead of only '404.html'

【讨论】:

  • 谢谢。我试过你的建议,但它仍然显示服务器 500。
猜你喜欢
  • 2019-07-10
  • 1970-01-01
  • 2018-12-27
  • 2010-11-04
  • 1970-01-01
  • 2014-08-02
  • 2012-04-06
  • 1970-01-01
  • 2016-09-29
相关资源
最近更新 更多