Introduction
An error appear upon executing a Django application. This article will discuss about how to solve that error message. Actually, the execution of the Django application is a success. But apparently, accessing it from a specific IP address will generate an error as in the title of this article. There is an error where the short error message in the page is :
DisallowedHost at / Invalid HTTP_HOST header: '192.168.1.1:8000'. You may need to add '192.168.1.1' to ALLOWED_HOSTS.
Suppose that the Django application is running in a server or a machine with the IP address of ‘192.168.1.1’. So, if there is another client or a machine acting as a client in the local network as in the same network segment, it will need to access using that IP address. But apparently, the Django application is appearing as it would normally. Instead, it is generating the above error message.
Solution
So, this article will how to solve the error message. After googling it using the error message as the keyword. The following is the actual part of the configuration line exist in ‘settings.py’ file. That file exist in the folder of the project as follows by describing in the structure from the root folder of the project:
[django@localhost ~]$ tree -L 2 . ├── apps │ ├── app │ ├── authentication │ ├── config.py │ ├── __init__.py │ └── __pycache__ ├── app.yaml ├── CHANGELOG.md ├── core │ ├── asgi.py │ ├── core │ ├── db.sqlite3 │ ├── __init__.py │ ├── __pycache__ │ ├── settings.py │ ├── static │ ├── staticfiles │ ├── templates │ ├── urls.py │ └── wsgi.py ├── db.sqlite3 ├── docker-compose.yml ├── Dockerfile ├── gunicorn-cfg.py ├── LICENSE.md ├── manage.py ├── media │ ├── django-dashboard-atlantis-dark-intro.gif │ ├── django-dashboard-atlantis-dark-screen-1.png │ ├── django-dashboard-atlantis-dark-screen-2.png │ ├── django-dashboard-atlantis-dark-screen-3.png │ ├── django-dashboard-atlantis-dark-screen-4.png │ ├── django-dashboard-atlantis-dark-screen-5.png │ ├── django-dashboard-atlantis-dark-screen-6.png │ ├── django-dashboard-atlantis-dark-screen-login.png │ ├── django-dashboard-atlantis-dark-screen.png │ └── django-dashboard-atlantis-dark-screen-register.png ├── models2.py ├── package.json ├── Procfile ├── README.md ├── requirements.txt ├── runtime.txt ├── staticfiles ├── admin ├── assets ├── ckeditor ├── favicon.ico └── sitemap.xml 27 directories, 36 files [django@localhost ~]$
That configuration part which is very important for solving the error message exist in the ‘settings.py’ file inside the project folder of ‘core’ as follows :
# load production server from .env ALLOWED_HOSTS = ['localhost', '127.0.0.1', config('SERVER', default='127.0.0.1')]
Although the above configuration revision has the following revision by adding the ‘*’ character as a wildcard for accepting any IP address form as follows :
# load production server from .env ALLOWED_HOSTS = ['localhost', '127.0.0.1', '*', config('SERVER', default='127.0.0.1')]
The above revision of the configuration line is not working. So, the last resort for solving the problem is by adding the actual IP address in the configuration line as follows :
# load production server from .env ALLOWED_HOSTS = ['localhost', '127.0.0.1', '*', config('SERVER', default='127.0.0.1'),'192.168.1.1']
Run it once more by refreshing or accessing the Django application web page.