Reputation: 2138
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?
Upvotes: 194
Views: 372836
Reputation: 2288
Sometimes this error appears out of nowhere, maybe after pulling latest code or after installing/configuring a new package, which is weird and none of the logical solutions shared work, I had this case.
In such cases, closely observe the recent code changes you have had especially in settings.py
. Try these steps:
settings.py
, mainly any new package/library that you configured, this may lead you to the package/library name which caused the problem.settings.py
is set in your env file, or you retrieve it with default value e.g. env('API_KEY', default="abc")
.In my case, one of my team member had configured cloudinary in settings.py
, after pulling latest code I got following changes:
cloudinary.config(
cloud_name=env("CLOUD_NAME"),
api_key=env("API_KEY"),
api_secret=env("API_SECRET"))
However, I did not have CLOUD_NAME, API_KEY, API_SECRET defined in my .env
file. And this was the root problem for me(we are using environ for env vars).
SOLUTION: I just defined these env variables(CLOUD_NAME="", API_KEY="", API_SECRET="") in my environment variable file .env
and it started working.
Upvotes: 0
Reputation: 838
You are getting this error because settings.py does not recognized by Pycharm correctly. Therefore you must specify your settings.py file in Preferences.
Go to: Preferences -> Languages & Frameworks -> Django ->
Enable Django Support
Django project root
to your main project folderSettings
to your main_project/your_app/settings.py
Manage script
to your main_folder/manage.py
Upvotes: 0
Reputation: 75
Faced the same error when using a .env file in development in a local setup on my computer which I didn't copy on the production server. As soon as I added the .env file on the production server the error was resolved. I have also added the server's IP Address to the allowed_hosts list on the settings file.
Upvotes: 0
Reputation: 1
Make sure the name of the directory is the same that you are mentioning in the INSTALLED_APPS. These are the solution to errors sometimes.
Upvotes: 0
Reputation: 180
So in my main directory in my django project, I had a two directories that had the same name so I deleted the settings folder I had and kept the settings.py file that comes with django.
What I had originally
<name-of-your-project-app>/
├── __init__.py
├── asgi.py
├── settings
│ ├── __init__.py
│ ├── base.py
│ └── development.py
├── settings.py
├── urls.py
└── wsgi.py
What I had afterwards
<name-of-your-project-app>/
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
Upvotes: 1
Reputation: 171
In my case I had split out my settings.py
into base.py
and development.py
in a settings folder. It looked something like:
<name-of-your-project-app>/
├── __init__.py
├── asgi.py
├── settings
│ ├── __init__.py
│ ├── base.py
│ └── development.py
├── urls.py
└── wsgi.py
The problem was much bigger than ALLOWED_HOSTS=... because none of the settings were recognized by python manage.py runserver
.
The fix was to configure the DJANGO_SETTINGS_MODULE by running
export DJANGO_SETTINGS_MODULE=decoupled_dj.settings.development
in the command line. I think this tells Django, 'Hey look for my settings in the development.py
'
Upvotes: 1
Reputation: 1
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:
manage.py
migratemanage.py
createsuperusermanage.py
runserver127.0.0.1:8000/Quit
127.0.0.1:8000/admin
Upvotes: -1
Reputation: 1432
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.example']
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
.
Upvotes: 84
Reputation: 21
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')
Upvotes: 2
Reputation: 439
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
Upvotes: 2
Reputation: 1349
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.
Upvotes: 0
Reputation: 59
This works for me:
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Upvotes: 3
Reputation: 1
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.
Upvotes: 0
Reputation: 2268
If you work in PyCharm
, check the Environmental variables
for your Django server
. You should specify the proper module.settings
file
Upvotes: 2
Reputation: 4495
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.
Upvotes: 258
Reputation: 193
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']
Upvotes: 17
Reputation: 3642
Make sure it's not redefined again lower down in your settings.py. The default settings has:
ALLOWED_HOSTS = []
Upvotes: 33