Python 当 DEBUG = False 时,Django 给出错误请求 (400)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19875789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django gives Bad Request (400) when DEBUG = False
提问by codeimplementer
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:
我是 django-1.6 的新手。当我使用 运行 django 服务器时DEBUG = True
,它运行完美。但是,当我改变DEBUG
到False
在设置文件,然后在服务器停止,并让在命令提示符下以下错误:
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:
更改ALLOWED_HOSTS
为 后["http://127.0.0.1:8000",]
,在浏览器中出现错误:
Bad Request (400)
Is it possible to run Django without debug mode?
是否可以在没有调试模式的情况下运行 Django?
采纳答案by Martijn Pieters
The ALLOWED_HOSTS
listshould contain fully qualified host names, noturls. 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
列表应包含完全限定的主机名,而不是url。省略端口和协议。如果您正在使用127.0.0.1
,我也会添加localhost
到列表中:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
You could also use *
to match anyhost:
您还可以使用*
匹配任何主机:
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
headerexactly (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
).
此列表中的值可以是完全限定名称(例如
'www.example.com'
),在这种情况下,它们将与请求的Host
标头完全匹配(不区分大小写,不包括 port)。用了一段开头的值可以用作一个子域通配符:'.example.com'
将匹配example.com
,www.example.com
以及任何其他子域example.com
。值'*'
将匹配任何内容;在这种情况下,您有责任提供您自己的Host
标头验证(可能在中间件中;如果是这样,则该中间件必须首先在 中列出MIDDLEWARE_CLASSES
)。
Bold emphasis mine.
大胆强调雷。
The status 400 response you get is due to a SuspiciousOperation
exceptionbeing raised when your host header doesn't match any values in that list.
您获得的状态 400 响应是由于当您的主机标头与该列表中的任何值都不匹配时引发的SuspiciousOperation
异常。
回答by Jorge
I had the same problem and I fixed it by setting ALLOWED_HOSTS = ['*']
and to solve the problem with the static images you have to change the virtual paths in the environment configuration like this:
我遇到了同样的问题,我通过设置ALLOWED_HOSTS = ['*']
和解决静态图像的问题来修复它,您必须像这样更改环境配置中的虚拟路径:
Virtual Path Directory
/static/ /opt/python/current/app/yourpj/static/
/media/ /opt/python/current/app/Nuevo/media/
虚拟路径 目录
/static/ /opt/python/current/app/yourpj/static/
/media/ /opt/python/current/app/Nuevo/media/
I hope it helps you.
我希望它能帮助你。
PD: sorry for my bad english.
PD:抱歉我的英语不好。
回答by Stephen Mwangi Wanjohi
Navigate to settings and locate the base.py file Set the allowed hosts to ALLOWED_HOSTS = ['*']
导航到设置并找到 base.py 文件将允许的主机设置为 ALLOWED_HOSTS = ['*']
回答by Keith
For me, I got this error by not setting USE_X_FORWARDED_HOST
to true. From the docs:
对我来说,由于未设置USE_X_FORWARDED_HOST
为 true ,我收到了此错误。从文档:
This should only be enabled if a proxy which sets this header is in use.
仅当使用设置此标头的代理时才应启用此功能。
My hosting service wroteexplicitly in their documentation that this setting mustbe used, and I get this 400 error if I forget it.
我的托管服务在他们的文档中明确写道必须使用此设置,如果我忘记了,我会收到此 400 错误。
回答by Abhishek Lodha
With DEBUG = False
in you settings file, you also need ALLOWED_HOST list set up.
Try including ALLOWED_HOST = ['127.0.0.1', 'localhost', 'www.yourdomain.com']
随着DEBUG = False
在你的设置文件,你还需要ALLOWED_HOST列表设置。尝试包括ALLOWED_HOST = ['127.0.0.1', 'localhost', 'www.yourdomain.com']
Otherwise you might receive a Bad Request(400) error from django.
否则,您可能会收到来自 django 的 Bad Request(400) 错误。
回答by Yuseferi
I had the same problem and none of the answers resolved my problem, for resolving the situation like this it's better to enable logging by adding the following config to settings.py
temporary
我遇到了同样的问题,但没有一个答案解决了我的问题,为了解决这样的情况,最好通过将以下配置添加到settings.py
临时来启用日志记录
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': '/tmp/debug.log', }, }, 'loggers': { 'django': { 'handlers': ['file'], 'level': 'DEBUG', 'propagate': True, }, }, }
and try to tail -f /tmp/debug.log
.
and when you see your issue you can handle it much easier than blind debugging.
并尝试tail -f /tmp/debug.log
。当你看到你的问题时,你可以比盲目调试更容易处理。
My issue was about to
我的问题即将
Invalid HTTP_HOST header: 'pt_web:8000'. The domain name provided is not valid according to RFC 1034/1035.
无效的 HTTP_HOST 标头:“pt_web:8000”。根据 RFC 1034/1035,提供的域名无效。
and resolve it by adding proxy_set_header Host $host;
to Nginx config file and enabling port forwarding by USE_X_FORWARDED_PORT = True
in the settings.py
( it's because in my case I've listened to request in Nginx on port 8080
and pass it to guni
on port 8000
并通过添加解决它proxy_set_header Host $host;
,以Nginx的配置文件,通过启用端口转发USE_X_FORWARDED_PORT = True
的settings.py
(这是因为在我的情况我已经听过请求Nginx的端口8080
,并把它传递给guni
上港8000
回答by Muhamed Noaman
For me as I have already xampp on 127.0.0.1 and django on 127.0.1.1 and i kept trying adding hosts
对我来说,因为我已经在 127.0.0.1 上安装了 xampp,在 127.0.1.1 上安装了 django,我一直在尝试添加主机
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.yourdomain.com', '*', '127.0.1.1']
and i got the same error or (400) bad request
我得到了同样的错误或 (400) 错误的请求
so I change the url to 127.0.1.1:(the used port)/project and voila !
所以我将 url 更改为 127.0.1.1:(the used port)/project ,瞧!
you have to check what is your virtual network address, for me as i use bitnami django stack 2.2.3-1 on Linux i can check which port django is using.
if you have an error ( 400 bad request ) then i guess django on different virtual network ..
good luck
你必须检查你的虚拟网络地址是什么,因为我在 Linux 上使用 bitnami django stack 2.2.3-1 我可以检查 django 正在使用哪个端口。如果你有错误(400个错误的请求)那么我猜django在不同的虚拟网络上..祝你好运
回答by Alberto
Try to run your server with the --insecure just like this:
尝试使用 --insecure 运行您的服务器,如下所示:
python manage.py runserver --insecure
python manage.py runserver --insecure