246
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.
80
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
.
- [Django]-Change a form value before validation in Django form
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-No module named pkg_resources
32
Make sure itβs not redefined again lower down in your settings.py. The default settings has:
ALLOWED_HOSTS = []
- [Django]-Redirect to Next after login in Django
- [Django]-Why does DEBUG=False setting make my django Static Files Access fail?
- [Django]-Django project models.py versus app models.py
17
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']
- [Django]-Why is logged_out.html not overriding in django registration?
- [Django]-How to use pdb.set_trace() in a Django unittest?
- [Django]-Negating a boolean in Django template
- [Django]-Django logging of custom management commands
- [Django]-Django β accessing the RequestContext from within a custom filter
- [Django]-Disable a method in a ViewSet, django-rest-framework
2
If you work in PyCharm
, check the Environmental variables
for your Django server
. You should specify the proper module.settings
file
- [Django]-Using django-rest-interface
- [Django]-Can I access constants in settings.py from templates in Django?
- [Django]-Serving Media files during deployment in django 1.8
2
This works for me:
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
- [Django]-What is a django.utils.functional.__proxy__ object and what it helps with?
- [Django]-How to mix queryset results?
- [Django]-How to solve "Page not found (404)" error in Django?
2
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
- [Django]-Parsing unicode input using python json.loads
- [Django]-Django model CharField: max_length does not work?
- [Django]-Form with CheckboxSelectMultiple doesn't validate
1
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')
- [Django]-Rendering a template variable as HTML
- [Django]-Django queries β id vs pk
- [Django]-Django simple_tag and setting context variables
1
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
β
- [Django]-Django: Filter a Queryset made of unions not working
- [Django]-What does error mean? : "Forbidden (Referer checking failed β no Referer.):"
- [Django]-Django-celery: No result backend configured
1
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
- [Django]-Django β SQL bulk get_or_create possible?
- [Django]-Django + Ajax
- [Django]-Django apps aren't loaded yet when using asgi
0
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.
- [Django]-Getting the SQL from a Django QuerySet
- [Django]-Django Framework β Is there a shutdown event that can be subscribed to?
- [Django]-How do I deploy Django on AWS?
0
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.
- [Django]-How to change site title, site header and index title in Django Admin?
- [Django]-Django celery task: Newly created model DoesNotExist
- [Django]-IOS app with Django
0
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.
- [Django]-Django rest framework, use different serializers in the same ModelViewSet
- [Django]-What is a "django backend"?
- [Django]-Django models: Only permit one entry in a model?
0
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.
- [Django]-Django β how to unit test a post request using request.FILES
- [Django]-Django: Safely Remove Old Migrations?
- [Django]-How to select a record and update it, with a single queryset in Django?
0
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
- Set
Django project root
to your main project folder - Set
Settings
to yourmain_project/your_app/settings.py
- Set
Manage script
to yourmain_folder/manage.py
- [Django]-How does the get_or_create function in Django return two values?
- [Django]-How to get the current URL within a Django template?
- [Django]-How to add superuser in Django from fixture
-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:
- 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
- [Django]-Python Asyncio in Django View
- [Django]-IOS app with Django
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-How to revert the last migration?
- [Django]-Automatic creation date for Django model form objects