我是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响应是由于当您的主机头与该列表中的任何值不匹配时引发了一个可疑操作异常。

我也有同样的问题,我通过设置ALLOWED_HOSTS =['*']来解决这个问题,为了解决静态映像的问题,你必须改变环境配置中的虚拟路径,就像这样:

虚拟路径                  目录 /静态 /                          / opt / python /经常/ app / yourpj /静态/ /媒体 /                        / opt / python /经常/ app /新/媒体/

我希望这对你有所帮助。

抱歉我的英语不好。

对我来说,我得到这个错误没有设置USE_X_FORWARDED_HOST为true。从文档中可以看出:

只有当设置此报头的代理正在使用时,才应该启用该选项。

我的主机服务在他们的文档中明确地写了这个设置必须使用,如果我忘记它,我就会得到这个400错误。

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

否则你可能会收到来自django的Bad Request(400)错误。

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

当您发现问题时,处理起来比盲目调试要容易得多。

我的问题是:

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

我通过添加proxy_set_header主机$ Host来解决它;在settings.py中启用USE_X_FORWARDED_PORT = True的端口转发(这是因为在我的情况下,我在Nginx端口8080上监听请求,并在端口8000上传递给guni)。

对于我来说,我已经在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:(使用的端口)/项目 瞧!

你必须检查你的虚拟网络地址是什么,对我来说,因为我在Linux上使用bitnami django stack 2.2.3-1,我可以检查django正在使用的端口。 如果你有一个错误(400坏请求),那么我猜django在不同的虚拟网络.. 祝你好运

试着用——insecure标志运行你的服务器,就像这样:

python manage.py runserver --insecure

我必须先停止apache服务器。

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

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

to ALLOWED_HOSTS = ['se.rv.er.]ip”、“www.example.com”)

在项目的settings.py中,检查第28行,其中是允许主机

settings.py


ALLOWED_HOSTS = ['IP', 'servidor', ]

你必须把IP和你使用的服务器,本地或web级别 settings.py

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.ejemplo.com']

or

ALLOWED_HOSTS = ['*']

尝试manage.py collectstatic。 我在更新后丢失了一个静态文件,因此出现了糟糕的请求。