Free auditStart free
GuideError · HTTP status codes

400 Bad Request in Django

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.

4 min
read
3
sources
Oct 31, 2026
updated
Scaplet Editorial Team
author
The short answer

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.

  1. Why a deployed Django site answers 400
  2. Reproduce and check it in 2 minutes
  3. Fixes by deployment shape
  4. Other causes behind a Django 400
  5. What it means for SEO and crawlers
30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
02Check it

Reproduce and check it in 2 minutes

A 400 is simple to compare against a known good one. This public endpoint always returns 400, so you can confirm your tooling prints the status the way you expect:

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):

Your own server: vary only the Host header
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.

Public endpoint: expect HTTP 400
curl -s -o /dev/null -w "%{http_code}\n" https://httpbin.org/status/400
# Expect: 400
03Pick the entry for your setup

Fixes by deployment shape

The matching rules in the settings reference: an exact name like www.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.
Setup

Single domain, direct to Django

What to add
The domain and the www variant
Watch out for
Reaching the server by raw IP sends the IP as Host, which then needs an entry too
Setup

Many tenant subdomains

What to add
A leading-dot entry such as .example.com
Watch out for
It also matches the bare domain
Setup

Load balancer health checks

What to add
The host name or IP the checker sends as Host
Watch out for
Checks that hit the instance IP will 400 until that value is listed
Setup

Proxy rewrites the Host header

What to add
Either list the internal name, or have the proxy pass the original Host
Watch out for
With USE_X_FORWARDED_HOST on, the forwarded value is the one checked
settings.py: a production list
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.

04Your call

Other causes behind a Django 400

Because the handler turns any 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.
If this fits

It is ALLOWED_HOSTS if

  • The error began right after setting DEBUG to False or moving to a new domain
  • The log shows a DisallowedHost exception
  • Changing only the Host header in curl flips the status
Or

Look elsewhere if

  • The Host values are all listed and the 400 persists
  • The traceback names a different SuspiciousOperation subclass
  • The response comes from a proxy or load balancer, not from Django
05Explained

What it means for SEO and crawlers

Google handles 400 under the same rule as other 4xx codes except 429: content it receives from those URLs is ignored. A site that returns 400 for a host name Googlebot uses, such as the bare domain or a www variant missing from the list, is invisible on that host. After fixing the setting, check the other 4xx pages too, such as 403 in Django and the general 400 Bad Request guide, and read 4xx errors for how to find affected URLs.
Sources · 3

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • djangoproject.com
  • google.com
  1. 01Django settings: ALLOWED_HOSTSdocs.djangoproject.com · checked 2026-11-01
  2. 02Django exceptions: SuspiciousOperationdocs.djangoproject.com · checked 2026-11-01
  3. 03How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Why does Django show Bad Request (400) when DEBUG is False?

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.

02Does ALLOWED_HOSTS include the port?

No. Exact names are matched against the Host header case-insensitively and without the port.

03What does DisallowedHost mean?

It is a SuspiciousOperation subclass raised when the host does not match ALLOWED_HOSTS. If it reaches the handler, the response is a 400.

Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane