110👍
In your settings.py
, there is a list called ALLOWED_HOSTS
. You need to add the IP address you see in the error to that list:
ALLOWED_HOSTS = ['XX.XX.XX.XX']
Note: only add the IP address, and not the port (e.g., 127.0.0.1
and not 127.0.0.1:8000
)
Explanation:
Django checks the Host
header of the HTTP request for a url/ip address that is within the allowed hosts.
From the django website:
This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.
https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
14👍
For development, you can use the *
wildcard to allow all hosts in settings.py
:
ALLOWED_HOSTS = [‘*’]
Important
Modify this configuration when you deploy your app in production environment.
- [Django]-Django middleware difference between process_request and process_view
- [Django]-Where should signal handlers live in a django project?
- [Django]-How do I access the request object or any other variable in a form's clean() method?
3👍
Include both (‘www.name.com’, ‘ip.ip.ip.ip’)
Set Debug = True, then retry the IP & URL Address.
Go to the Traceback section, find the message [ raise DisallowedHost(msg) ]
click -> ▼ Local vars
It will show the incoming domain name and the settings for allowed hosts:
*Variable Value
*allowed_hosts ['ip.ip.ip.ip', 'name.com']
*domain 'something.com'
*
Copy the incoming value into your settings.py. If the you see old settings restart the server\nginx
- [Django]-How to create password input field in django
- [Django]-Programmatically saving image to Django ImageField
- [Django]-What is the difference render() and redirect() in Django?
1👍
Sometimes is not enough to just add it to the host as a frustratedly tried over and over. Sometimes is stuck in cache and you’re getting the same error even if you did everything right. In that case what worked for me is change the port, from 8081 and cache problem was over.
I ran it like this:
python3 manage.py runserver 127.0.0.1:8081
- [Django]-Why does django run everything twice?
- [Django]-Tailwindcss: fixed/sticky footer on the bottom
- [Django]-Django add extra field to a ModelForm generated from a Model
0👍
For Run Django Project on localhost with free hosting by “ngrok”
run ngrok http 8000
(before run this in your project make sure your project are required to run on localhost like- python manage.py runserver)
http://563ae936.ngrok.io -> http://localhost:8000
Edit Setting.py
ALLOWED_HOSTS = [‘563ae936.ngrok.io’, ‘localhost’, ‘127.0.0.1’, ‘testserver’]
Here “563ae936.ngrok.io” Replace your Host name with removing http:// or https://
- [Django]-How can I test https connections with Django as easily as I can non-https connections using 'runserver'?
- [Django]-How to filter objects for count annotation in Django?
- [Django]-How to completely dump the data for Django-CMS
- [Django]-Itertools.groupby in a django template
- [Django]-How can I disable logging while running unit tests in Python Django?
- [Django]-Model not showing up in django admin