当DEBUG=Fals时,Django发出错误请求(400)

2024-04-29 10:21:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的django-1.6。当我用DEBUG = True运行django服务器时,它运行得很好。但是,当我在设置文件中将DEBUG更改为False时,服务器将停止,并在命令提示符上显示以下错误:

CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

在我将ALLOWED_HOSTS更改为["http://127.0.0.1:8000",]之后,在浏览器中我得到错误:

Bad Request (400)

是否可以在没有调试模式的情况下运行Django?


Tags: 文件djangodebug服务器youfalsetrue错误
3条回答

对我来说,这个错误是因为没有将^{}设置为true。从文档中:

This should only be enabled if a proxy which sets this header is in use.

我的托管服务wrote在他们的文档中明确指出,必须使用此设置,如果忘记了,我将得到400个错误。

我也遇到了同样的问题,我通过设置ALLOWED_HOSTS = ['*']解决了这个问题,为了解决静态映像的问题,您必须更改环境配置中的虚拟路径,如下所示:

虚拟路径目录

/静态的//opt/python/current/app/yourpj/static/
/媒体//opt/python/current/app/numo/media/

希望对你有帮助。

警察:对不起我英语不好。

^{} list应该包含完全限定的主机名而不是url。去掉端口和协议。如果您使用的是127.0.0.1,我也会将localhost添加到列表中:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

您还可以使用*来匹配任何宿主:

ALLOWED_HOSTS = ['*']

引用文档:

Values in this list can be fully qualified names (e.g. 'www.example.com'), in which case they will be matched against the request’s Host header exactly (case-insensitive, not including port). A value beginning with a period can be used as a subdomain wildcard: '.example.com' will match example.com, www.example.com, and any other subdomain of example.com. A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE_CLASSES).

加粗强调我的

您得到的状态400响应是由于当主机头与该列表中的任何值不匹配时引发了^{} exception

相关问题 更多 >