I'm using Django 1.6.5 with the setting:
DEBUG = True
When I change to DEBUG = False
and run manage.py runserver
, I get the following error:
CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
I get the same error with the following setting:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
How can I fix this?
This question is related to
django
If you work in PyCharm
, check the Environmental variables
for your Django server
. You should specify the proper module.settings
file
Try
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*']
A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header.
Make sure it's not redefined again lower down in your settings.py. The default settings has:
ALLOWED_HOSTS = []
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']
The condition to be satisfied is that the host header (or X-Forwarded-Host
if USE_X_FORWARDED_HOST
is enabled) should match one of the values in ALLOWED_HOSTS
.
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.
EDIT: This is due to a Django security update as mentioned in my comment.
I also experienced this cmderror. After trying all the answers on here, I couldn't still figure out the problem, here is what I did:
This works for me:
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Use this:
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Just simply comment out the line: ALLOWED_HOSTS = [...]
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']
Source: Stackoverflow.com