我是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吗?
ALLOWED_HOSTS列表应该包含完全限定的主机名,而不是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响应是由于当您的主机头与该列表中的任何值不匹配时引发了一个可疑操作异常。