Disallowed Host at Django
Question:
I’m just starting out with Django for the first time. So I create a django project and run the command
python3 manage.py runserver 0.0.0.0:8000
Instead of getting the expected django homepage, I get the following error message,
DisallowedHost at /
Invalid HTTP_HOST header: '0.0.0.0:8000'. You may need to add '0.0.0.0' to ALLOWED_HOSTS.
Request Method: GET
Request URL: http://0.0.0.0:8000/
Django Version: 1.10.5
Exception Type: DisallowedHost
Exception Value:
Invalid HTTP_HOST header: '0.0.0.0:8000'. You may need to add '0.0.0.0' to ALLOWED_HOSTS.
Exception Location: /usr/local/lib/python3.5/site-packages/django/http/request.py in get_host, line 113
Python Executable: /usr/local/opt/python3/bin/python3.5
Python Version: 3.5.2
I haven’t yet scratched the surface of django, so would appreciate some help as to how to fix this?
Answers:
literally as the error suggested! go ahead and add the line
0.0.0.0
to the ALLOWED_HOSTS
in your settings.py
The error info is quite explicit. It should solve the problem right away.
I too had problems with this and it is not easy for a beginner to fix. Here below is the sequence required:
First find your settings.py file, which is located:
projectName/projectName/settings.py
You can also find it using:
find -name “settings.py”
– likely you will have only one!
Once you have found this, you need to open it in a text editor and change the line
ALLOWED_HOSTS =[ ] to ALLOWED_HOSTS =[‘your.host.ip.address’] – include the ‘ ‘ and the [ ] then save.
You will have to re-start the server to get this to work eg:
python manage.py runserver your.host.ip.address:8000
If you get a syntax error, make sure you have something that looks like:
ALLOWED_HOSTS =['192.168.1.200']
or whatever your ip address is.
1.You can also find it using: find -name “SETTINGS.py” – likely you will have only one!
2.Once you have found this, you need to open it in a text editor and change the line in the insetting File
ALLOWED_HOSTS =['Your_compte_pythonanywher.pythonanywhere.com']
Just close the postman and try request in new tab. This worked for me.
Additionally, you can have a file local_settings.py and settings.py.
If you are running in a local or a VPS server considered as local server:
python manage.py runserver 0.0.0.0:8001
You need to add in local_settings.py
Also you can search how many ALLOWED_HOSTS has your code with:
grep -rin ALLOWED_HOSTS your_source_path
I’m just starting out with Django for the first time. So I create a django project and run the command
python3 manage.py runserver 0.0.0.0:8000
Instead of getting the expected django homepage, I get the following error message,
DisallowedHost at /
Invalid HTTP_HOST header: '0.0.0.0:8000'. You may need to add '0.0.0.0' to ALLOWED_HOSTS.
Request Method: GET
Request URL: http://0.0.0.0:8000/
Django Version: 1.10.5
Exception Type: DisallowedHost
Exception Value:
Invalid HTTP_HOST header: '0.0.0.0:8000'. You may need to add '0.0.0.0' to ALLOWED_HOSTS.
Exception Location: /usr/local/lib/python3.5/site-packages/django/http/request.py in get_host, line 113
Python Executable: /usr/local/opt/python3/bin/python3.5
Python Version: 3.5.2
I haven’t yet scratched the surface of django, so would appreciate some help as to how to fix this?
literally as the error suggested! go ahead and add the line
0.0.0.0
to the ALLOWED_HOSTS
in your settings.py
The error info is quite explicit. It should solve the problem right away.
I too had problems with this and it is not easy for a beginner to fix. Here below is the sequence required:
First find your settings.py file, which is located:
projectName/projectName/settings.py
You can also find it using:
find -name “settings.py”
– likely you will have only one!
Once you have found this, you need to open it in a text editor and change the line
ALLOWED_HOSTS =[ ] to ALLOWED_HOSTS =[‘your.host.ip.address’] – include the ‘ ‘ and the [ ] then save.
You will have to re-start the server to get this to work eg:
python manage.py runserver your.host.ip.address:8000
If you get a syntax error, make sure you have something that looks like:
ALLOWED_HOSTS =['192.168.1.200']
or whatever your ip address is.
1.You can also find it using: find -name “SETTINGS.py” – likely you will have only one!
2.Once you have found this, you need to open it in a text editor and change the line in the insetting File
ALLOWED_HOSTS =['Your_compte_pythonanywher.pythonanywhere.com']
Just close the postman and try request in new tab. This worked for me.
Additionally, you can have a file local_settings.py and settings.py.
If you are running in a local or a VPS server considered as local server:
python manage.py runserver 0.0.0.0:8001
You need to add in local_settings.py
Also you can search how many ALLOWED_HOSTS has your code with:
grep -rin ALLOWED_HOSTS your_source_path