Disallowed Host at Django

Disallowed Host at Django

If you have been developing a Django app and you encounter a "Disallowed Host" error, don't worry, it's a common issue. This error occurs when a request comes from a host that is not allowed in your Django settings file.

Causes of Disallowed Host Errors

  • Incorrectly defined ALLOWED_HOSTS setting in Django settings file.
  • Request coming from a host that is not listed in ALLOWED_HOSTS.
  • Host header is being added/modified in middlewares or reverse proxies.

Solutions to Disallowed Host Errors

To solve this issue, you need to check your settings file and confirm that the ALLOWED_HOSTS parameter is correctly defined.

Solution 1: Edit your settings.py file and add your domain or IP address to the ALLOWED_HOSTS parameter.


ALLOWED_HOSTS = ['example.com', '192.168.1.1']

Solution 2: If you don't know your IP address, you can set ALLOWED_HOSTS to '*' to allow all hosts. However, this is not recommended in production environments.


ALLOWED_HOSTS = ['*']

Solution 3: Check if the request is coming from a host that is not listed in ALLOWED_HOSTS. For example, if your app is running on 'example.com' and someone tries to access it from 'example.net', it will result in a Disallowed Host error.

Solution 4: If you are using a reverse proxy, make sure that the host header is being added/modified correctly. Django needs to receive the correct value of the host header to match it with ALLOWED_HOSTS.

That's it! These are the common solutions to Disallowed Host errors in Django. With these solutions, you should be able to easily fix this problem and continue developing your app.