Django渲染模板`500.html`而不是`404.html` [英] Django renders the template `500.html` instead of `404.html`

查看:609
本文介绍了Django渲染模板`500.html`而不是`404.html`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个意见中,我有代码:

  raise Http404 

DEBUG = False Django呈现模板 500.html 而不是正确的 404.html !我不明白为什么!



编辑:当 DEBUG = True 我得到标准的(由Django)

 找不到页面(404)
看到此错误是因为您的Django设置中有DEBUG = True文件。将其更改为False,Django将显示标准的404页面。

更多的是在 runserver 控制台我清楚地看到 404 代码。 ,当在控制台中设置 DEBUG = False 时,我得到一个 500 !这是非常奇怪的。



编辑2:如果我在提起之前放置打印声明Http404 我看到消息当 DEBUG = True 而不是 False



<编辑3:我可以确认,当 DEBUG = False 时,从未达到 raise Http404 的声明。如何才能实现?



更新



实际上与 DEBUG = False 在每个URL我得到一个500.而使用 DEBUG = True 这不会发生。怎么可能呢?它应该运行相同。我开始认为这是一个Django错误。

解决方案

您的错误与debug = false可能是由于allowed_hosts设置。



请参阅:设置DEBUG = False导致500错误



在django 1.5中是新的


生产所需的ALLOWED_HOSTS



新的ALLOWED_HOSTS设置验证请求的主机头,
可防止主机中毒攻击。当DEBUG为False时,此设置现在需要
,否则django.http.HttpRequest.get_host()
将引发SuspiciousOperation。有关详细信息,请参阅新设置的
文件。


https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed -hosts-required-in-production


In one of my views I have the code:

raise Http404

When DEBUG=False Django renders the template 500.html instead of the correct 404.html! I can't understand why!

EDIT: When DEBUG=True I get the standard one (by Django)

Page not Found (404)
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

What's more is that in the runserver console I see clearly the 404 code. Instead, when setting DEBUG=False in the console I get a 500!! This is so odd.

EDIT 2: If I place a print statement just before raising Http404 I see the message when DEBUG=True but not when it is False!

EDIT 3: I can confirm that when DEBUG=False the statement raise Http404 is never reached. How can it be possible??

Update

Actually with DEBUG=False at every URL I get a 500. While with DEBUG=True this does not happen. How can it possibly be?? It should run the same. I'm starting to think it is a Django bug.

解决方案

Your error with debug=false may be due to the allowed_hosts setting.

see : Setting DEBUG = False causes 500 Error

It is new in django 1.5

ALLOWED_HOSTS required in production

The new ALLOWED_HOSTS setting validates the request’s Host header and protects against host-poisoning attacks. This setting is now required whenever DEBUG is False, or else django.http.HttpRequest.get_host() will raise SuspiciousOperation. For more details see the full documentation for the new setting.

https://docs.djangoproject.com/en/1.5/releases/1.5/#allowed-hosts-required-in-production

这篇关于Django渲染模板`500.html`而不是`404.html`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆