CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
DjangoDjango Problem Overview
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?
Django Solutions
Solution 1 - Django
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.
Solution 2 - Django
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
.
Solution 3 - Django
Make sure it's not redefined again lower down in your settings.py. The default settings has:
ALLOWED_HOSTS = []
Solution 4 - Django
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']
Solution 5 - Django
Use this:
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Solution 6 - Django
If you work in PyCharm
, check the Environmental variables
for your Django server
. You should specify the proper module.settings
file
Solution 7 - Django
This works for me:
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Solution 8 - Django
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.
Solution 9 - Django
I had set ALLOW_HOSTS, INTERNAL_IPS and DEBUG=TRUE
but still got this error. my problem was i had created a python package which its name was 'settings' in main app. and that package name interfered with 'settings.py' file.
Solution 10 - Django
If you are using PyCharm
This solution applies only if you are using a different settings.py
and have environment variables set
I had the same issue, but in my case the issue was, I was using a different settings.py
file than the default (and had commented out my whole original settings.py
), though I had it properly configured in my manage.py
but in PyCharm I had to configure it as well in my Environment Variables via:
Edit Run Configurations >> Environment Variables
Solution 11 - Django
I also experienced the same error and found it is happening due to settings file configuration change. You have to configured few things as below mentioned.
Try
In settings.py
ALLOWED_HOSTS = ['*']
In manage.py
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your-project-name>.settings')
In asgi.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your-project-name>.settings')
In wsgi.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your-project-name>.settings')
Solution 12 - Django
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:
- Cd into the project directory. e.g cd project-dir
- I migrated. e.g python manage.py migrate
- I created a super user. e.g python manage.py createsuperuser
- Enter the desired info like username, password, email etc
- You should get a "super user created successfully" response
- Now run the server. E.g python manage.py runserver
- Click on the URL displayed
- The URL on your browser should look like this, 127.0.0.1:8000/Quit
- Now edit the URL on your browser to this, 127.0.0.1:8000/admin
- You should see an administrative login page
- Login with the super user info you created earlier on
- You should be logged in to the Django administration
- Now click on "view site" at the top of the page
- You should see a page which shows "the install worked successfully..... Debug = True"
- Voila! your server is up and running
Solution 13 - Django
Just simply comment out the line: ALLOWED_HOSTS = [...]