当DEBUG = False时,Django给出了错误请求(400) [英] Django gives Bad Request (400) when DEBUG = False
问题描述
DEBUG = True
运行django服务器时,它运行正常。但是,当我在设置文件中将 DEBUG
更改为 False
时,服务器停止,并在命令提示符: CommandError:如果DEBUG为False,则必须设置settings.ALLOWED_HOSTS。
将$ code> ALLOWED_HOSTS 更改为 [http://127.0.0.1:8000,]
,在浏览器中我收到错误:
错误请求(400)
是否可以在没有调试模式的情况下运行Django ?
ALLOWED_HOSTS
列表应包含完全限定的主机名,强>网址。留出端口和协议。如果您使用 127.0.0.1
,我将添加 localhost
到列表中:
ALLOWED_HOSTS = ['127.0.0.1','localhost']
您还可以使用 *
匹配任何主机:
ALLOWED_HOSTS = ['*']
引用文件:
此列表中的值可以是完全限定名称(例如
'www.example.com'
),在这种情况下,它们将与请求的主机
头匹配(不区分大小写,不包括端口)。以周期开头的值可以用作子域通配符:'。example.com'
将匹配example.com
,www.example.com
以及example.com
的任何其他子域名。值'*'
将匹配任何内容;在这种情况下,您有责任提供自己对主机
标头的验证(可能在中间件中);如果是这样,则该中间件必须首先列在MIDDLEWARE_CLASSES
粗体强调我的。
您获得的状态400响应是由于 SuspiciousOperation
exception 。
I am new to django-1.6. When I run the django server with DEBUG = True
, it's running perfectly. But when I change DEBUG
to False
in the settings file, then the server stopped and it gives the following error on the command prompt:
CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
After I changed ALLOWED_HOSTS
to ["http://127.0.0.1:8000",]
, in the browser I get the error:
Bad Request (400)
Is it possible to run Django without debug mode?
The ALLOWED_HOSTS
list should contain fully qualified host names, not urls. Leave out the port and the protocol. If you are using 127.0.0.1
, I would add localhost
to the list too:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
You could also use *
to match any host:
ALLOWED_HOSTS = ['*']
Quoting the documentation:
Values in this list can be fully qualified names (e.g.
'www.example.com'
), in which case they will be matched against the request’sHost
header exactly (case-insensitive, not including port). A value beginning with a period can be used as a subdomain wildcard:'.example.com'
will matchexample.com
,www.example.com
, and any other subdomain ofexample.com
. A value of'*'
will match anything; in this case you are responsible to provide your own validation of theHost
header (perhaps in a middleware; if so this middleware must be listed first inMIDDLEWARE_CLASSES
).
Bold emphasis mine.
The status 400 response you get is due to a SuspiciousOperation
exception being raised when your host header doesn't match any values in that list.
这篇关于当DEBUG = False时,Django给出了错误请求(400)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!