问题:当DEBUG = False时,Django给出错误请求(400)

我是django-1.6的新手。当我使用运行django服务器时DEBUG = True,它运行良好。但是,当我改变DEBUGFalse在设置文件,然后在服务器停止,并让在命令提示符下以下错误:

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

更改ALLOWED_HOSTS为之后["http://127.0.0.1:8000",],在浏览器中出现错误:

Bad Request (400)

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

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?


回答 0

应包含标准主机名而不是 URL。省略端口和协议。如果您使用127.0.0.1,我也将添加localhost到列表中:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

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

ALLOWED_HOSTS = ['*']

引用文档:

此列表中的值可以是完全限定的名称(例如'www.example.com'),在这种情况下,它们将与请求的Host标头完全匹配(不区分大小写,不包括port)。用了一段开头的值可以用作一个子域通配符:'.example.com'将匹配example.comwww.example.com以及任何其他子域example.com。值'*'将匹配任何内容;在这种情况下,您有责任提供自己的Host标头验证(可能在中间件中;如果是,则必须在中首先列出该中间件MIDDLEWARE_CLASSES)。

大胆强调我

您收到的状态400响应是由于主机标头与该列表中的任何值都不匹配时引发的SuspiciousOperation异常

The 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’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).

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.


回答 1

对我来说,我没有设置USE_X_FORWARDED_HOST为true便得到了这个错误。从文档:

仅当使用设置此标头的代理时,才应启用此功能。

我的托管服务在其文档中明确表示,该设置必须被使用,如果我忘了我得到这个400错误。

For me, I got this error by not setting USE_X_FORWARDED_HOST to true. From the docs:

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

My hosting service wrote explicitly in their documentation that this setting must be used, and I get this 400 error if I forget it.


回答 2

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

虚拟路径 目录

/ static / / opt / python / current / app / yourpj / static /
/ media / / opt / python / current / app / Nuevo / media /

希望对您有帮助。

PD:对不起,我的英语不好。

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:

Virtual Path Directory

/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.


回答 3

我遇到了同样的问题,没有一个答案可以解决我的问题,为了解决这种情况,最好通过在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, }, }, }

并尝试tail -f /tmp/debug.log。当您看到问题时,比盲目调试要容易得多。

我的问题即将

无效的HTTP_HOST标头:“ pt_web:8000”。根据RFC 1034/1035,提供的域名无效。

并通过添加解决它proxy_set_header Host $host;,以Nginx的配置文件,通过启用端口转发USE_X_FORWARDED_PORT = Truesettings.py(这是因为在我的情况我已经听过请求Nginx的端口8080,并把它传递给guni上港8000

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

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.

My issue was about to

Invalid HTTP_HOST header: ‘pt_web:8000’. The domain name provided is not valid according to 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


回答 4

对我来说,我已经在127.0.0.1上使用了xampp,在127.0.1.1上使用了django,并且我一直在尝试添加主机

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.yourdomain.com', '*', '127.0.1.1']

并且我遇到相同的错误或(400)错误的请求 在此处输入图片说明

所以我将URL更改为127.0.1.1 :(使用的端口)/ project和瞧!

您必须检查您的虚拟网络地址是什么,对我来说,因为我在Linux上使用bitnami django堆栈2.2.3-1,所以我可以检查django使用的端口。如果您有一个错误(400错误的请求),那么我猜django在不同的虚拟网络上..祝您好运 在此处输入图片说明

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

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.yourdomain.com', '*', '127.0.1.1']

and i got the same error or (400) bad request enter image description here

so I change the url to 127.0.1.1:(the used port)/project and voila !

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 enter image description here


回答 5

随着DEBUG = False在你的设置文件,你还需要ALLOWED_HOST列表设置。尝试包括ALLOWED_HOST = ['127.0.0.1', 'localhost', 'www.yourdomain.com']

否则,您可能会收到来自django的Bad Request(400)错误消息。

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']

Otherwise you might receive a Bad Request(400) error from django.


回答 6

尝试使用–insecure运行服务器,如下所示:

python manage.py runserver-不安全

Try to run your server with the –insecure just like this:

python manage.py runserver –insecure


回答 7

我必须先停止apache服务器。

(fe sudo systemctl stop httpd.service/ sudo systemctl disable httpd.service)。

除了编辑’ settings.py‘文件,这解决了我的问题

ALLOWED_HOSTS = ['se.rv.er.ip', 'www.example.com']

I had to stop the apache server first.

(f.e. sudo systemctl stop httpd.service / sudo systemctl disable httpd.service).

That solved my problem besides editing the ‘settings.py‘ file

to ALLOWED_HOSTS = ['se.rv.er.ip', 'www.example.com']


回答 8

导航到设置并找到base.py文件将允许的主机设置为ALLOWED_HOSTS = [‘*’]

Navigate to settings and locate the base.py file Set the allowed hosts to ALLOWED_HOSTS = [‘*’]


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。