# 400 Bad Request in Django

> Fix Django

Canonical: https://scaplet.com/errors/http/400/django

Guide Error · HTTP status codes

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

400 4xx · Client error

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](#why-a-deployed-django-site-answers-400)
2. [Reproduce and check it in 2 minutes](#reproduce-and-check-it-in-2-minutes)
3. [Fixes by deployment shape](#fixes-by-deployment-shape)
4. [Other causes behind a Django 400](#other-causes-behind-a-django-400)
5. [What it means for SEO and crawlers](#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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F400%2Fdjango%20(%22400%20Bad%20Request%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F400%2Fdjango%20(%22400%20Bad%20Request%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F400%2Fdjango%20(%22400%20Bad%20Request%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F400%2Fdjango%20(%22400%20Bad%20Request%20in%20Django%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_How ALLOWED_HOSTS is evaluated

## Why a deployed Django site answers 400

The classic story: the site works with `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)'."

Situation

### DEBUG is True and the list is empty

What Django does

Validates the host against ['.localhost', '127.0.0.1', '[::1]']

Situation

### DEBUG is False and the list is empty

What Django does

Rejects every request with a 400

Situation

### Host matches an entry

What Django does

Request proceeds; matching ignores case and the port

Situation

### No entry matches

What Django does

get_host() raises SuspiciousOperation, which reaches the handler as a 400

Situation

### USE_X_FORWARDED_HOST is on

What Django does

X-Forwarded-Host is validated in place of Host

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.

_02_Check 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
```

_03_Pick 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 ]
```

**Be careful with the wildcard**Using `'*'` turns the check off. The docs say you become "responsible to provide your own validation of the `Host` header". Prefer an explicit list.

The check only runs through `get_host()`. The docs warn that code reading the Host header straight from `request.META` bypasses this protection.

_04_Your 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

_05_Explained

## 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](/ai-crawlers/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](/errors/http/403/django) and the general [400 Bad Request](/errors/http/400) guide, and read [4xx errors](/seo-issues/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. 01 [Django settings: ALLOWED_HOSTS](https://docs.djangoproject.com/en/5.1/ref/settings/) docs.djangoproject.com · checked 2026-11-01
2. 02 [Django exceptions: SuspiciousOperation](https://docs.djangoproject.com/en/5.1/ref/exceptions/) docs.djangoproject.com · checked 2026-11-01
3. 03 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-11-01

Questions

## Asked a lot.

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

_01_ Why 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.

_02_ Does ALLOWED_HOSTS include the port?

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

_03_ What 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.

## Keep learning

[All errors](/errors)

[Learn 403 Forbidden in Django HTTP status codes](/errors/http/403/django) [Related 4xx errors Status codes and links](/seo-issues/4xx-errors) [Read next 404 Not Found in Django HTTP status codes](/errors/http/404/django) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 400 errors
[HTTP 400 Bad Request](/errors/http/400) [400 Bad Request in Apache httpd](/errors/http/400/apache) [400 Bad Request in Chrome](/errors/http/400/chrome) [400 Bad Request in Flask](/errors/http/400/flask) [400 Bad Request in nginx](/errors/http/400/nginx)

More in HTTP 400 Bad Request
[400 Bad Request in Apache httpd](/errors/http/400/apache) [400 Bad Request in Chrome](/errors/http/400/chrome) [400 Bad Request in Django](/errors/http/400/django) [400 Bad Request in Flask](/errors/http/400/flask) [400 Bad Request in nginx](/errors/http/400/nginx) [400 Bad Request in Spring Boot](/errors/http/400/spring-boot)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
