可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I use Django 1.6.5 in my program txsite with the settings:
DEBUG = True
I set DEBUG to False, but when I runserver
, I get the following error:
CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
I tried setting ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
but I get the same error.
anyone know how to figure it out?
回答1:
Try
ALLOWED_HOSTS = ['*']
Less secure if you're not firewalled off or on a public LAN, but it's what I use and it works.
EDIT: Interestingly enough I've been needing to add this to a few of my 1.8 projects even when DEBUG = True
. Very unsure why.
回答2:
Your solution might be to add the original IP and/or hostname also:
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '111.222.333.444', 'mywebsite.com']
Basically the host header (or X-Forwarded-Host
if USE_X_FORWARDED_HOST
is enabled) should match one of the values in ALLOWED_HOSTS
.
回答3:
Make sure it's not redefined again lower down in your settings.py. The default settings has:
ALLOWED_HOSTS = []
回答4:
From documentation: https://docs.djangoproject.com/en/1.10/ref/settings/
if DEBUG is False, you also need to properly set the ALLOWED_HOSTS setting. Failing to do so will result in all requests being returned as “Bad Request (400)”.
And from here: https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-ALLOWED_HOSTS
I am using something like this:
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'www.mysite.com']
回答5:
Just do this:
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
It will do the trick.
回答6:
Just simply comment out the line: ALLOWED_HOSTS = [...]