shaare.it

DisallowedHost: Invalid HTTP_HOST header

7 Dec 2025

1 min read

DisallowedHost: Invalid HTTP_HOST header

$ python -c "from django.http import HttpRequest; r=HttpRequest(); r.META['HTTP_HOST']='example.com'"
# Error usually appears during a request:
# django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'example.com'. You may need to add 'example.com' to ALLOWED_HOSTS.

Why this happens

Incoming requests use a host not listed in ALLOWED_HOSTS. Django rejects such requests for security.

Fix

Add the host/domain to ALLOWED_HOSTS or use ['*'] in trusted environments (not recommended for production).

Wrong code

# settings.py
ALLOWED_HOSTS = []

Fixed code

# settings.py
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "example.com"]