DEBUG is True and the list is empty
- What Django does
- Validates the host against ['.localhost', '127.0.0.1', '[::1]']
Fix Django's Bad Request (400): a Host header missing from ALLOWED_HOSTS, X-Forwarded-Host rules and other SuspiciousOperation causes, with settings and curl checks.
A Django 400 Bad Request almost always means the Host header did not match ALLOWED_HOSTS, so get_host() raised a SuspiciousOperation. With DEBUG off and ALLOWED_HOSTS empty, every request gets a 400. Add the exact domain, a leading-dot wildcard, or your load balancer's host name to ALLOWED_HOSTS.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
runserver, you set DEBUG = False for production, and every page now says Bad Request (400). The settings reference explains it. ALLOWED_HOSTS defaults to an empty list, and with DEBUG off an unset value "will result in all requests being returned as 'Bad Request (400)'."The exceptions reference says a SuspiciousOperation that reaches the ASGI or WSGI handler is logged at the Error level and results in an HttpResponseBadRequest. DisallowedHost is one of its subclasses, so the visitor sees a bare 400 and the detail stays in your logs.
To prove the Host header is the cause, send your own domain's IP address with different Host values. This needs your deployed site (replace the IP and names):
curl -s -o /dev/null -w "%{http_code}\n" -H 'Host: www.example.com' http://203.0.113.10/
curl -s -o /dev/null -w "%{http_code}\n" -H 'Host: not-listed.example.net' http://203.0.113.10/
# Expect: 200 for a name in ALLOWED_HOSTS, 400 for the other. If both print 400,
# a name you actually use is missing from the list.Your application log should record the exception. Find the DisallowedHost entry, note the host it reports, and decide whether that name belongs in the setting.
curl -s -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/400
# Expect: 400www.example.com matches case-insensitively and without the port; a leading period matches the domain and every subdomain; '*' matches anything. A trailing dot that some browsers add is stripped.DEBUG = False
ALLOWED_HOSTS = [
"example.com", # exact match
".example.com", # example.com and all subdomains
]The check only runs through get_host(). The docs warn that code reading the Host header straight from request.META bypasses this protection.
SuspiciousOperation into a 400, ALLOWED_HOSTS is the most common source but not the only one. The exceptions page gives a tampered session cookie as another example. When the log line is not DisallowedHost, read the exception class named in the traceback.3 questions people ask next, answered from the sources on this page.
ALLOWED_HOSTS defaults to an empty list. With DEBUG off, an unset list makes Django reject every request with a 400, so add your domain names to it.
No. Exact names are matched against the Host header case-insensitively and without the port.
It is a SuspiciousOperation subclass raised when the host does not match ALLOWED_HOSTS. If it reaches the handler, the response is a 400.
Run the free audit, then approve the fixes. Every change can be undone.