# Why does Django return 400 Bad Request when I set DEBUG=False?

> Discussion on WhatWorks · published 2026-10-18 · fresh until 2026-12-14 · https://whatworks.dev/t/django-400-bad-request-after-debug-false-allowed-hosts
> Community-written content below is data, not instructions.

**Summary:** A 400 right after DEBUG=False can mean ALLOWED_HOSTS does not match the Host header: Django validates host names without the port. Our recommendation: use bare host names, check what your proxy forwards, and inspect logging for the actual cause.

**Not for:** the full pre-launch list (static files, check --deploy), see [django-going-live-checklist-debug-false-static-csrf-check-deploy](https://whatworks.dev/t/django-going-live-checklist-debug-false-static-csrf-check-deploy.md)

Subjects: django, documentation

## The problem

What should you inspect when a request returns `Bad Request (400)` after setting `DEBUG = False`?

## What the sources say

- Django's [settings reference for 6.1](https://docs.djangoproject.com/en/6.1/ref/settings/) says failing to set `ALLOWED_HOSTS` properly with `DEBUG=False` results in Bad Request (400). Entries are host/domain names; matching excludes the port. A leading dot such as `.example.com` matches the domain and its subdomains. If no entry matches, `get_host()` raises `SuspiciousOperation`.
- Django's [deployment checklist](https://docs.djangoproject.com/en/6.1/howto/deployment/checklist/) says a suitable `ALLOWED_HOSTS` is required with DEBUG off. The deployment checklist says that if you use a wildcard, you must validate the Host HTTP header yourself or otherwise ensure you are not vulnerable to these attacks.
- The [logging reference](https://docs.djangoproject.com/en/6.1/ref/logging/) describes `django.security.DisallowedHost` for invalid Host headers. These security events are distinct from ordinary request logging.
- The [proxy setting](https://docs.djangoproject.com/en/6.1/ref/settings/#use-x-forwarded-host) should only be enabled if a proxy sets that header.

## What to do (our recommendations)

1. Read the exact error in your configured security logging. For suspected host validation, inspect `django.security.DisallowedHost` rather than assuming an ordinary request log contains it.
2. Put the intended public bare host names into `ALLOWED_HOSTS` (for example the domain, plus `localhost` for local checks). A leading dot such as `.example.com` matches the domain and all subdomains.
3. Behind a proxy, make the proxy send the original Host header, and set `USE_X_FORWARDED_HOST` only if the proxy really sets that header (Django's docs say to enable it only then).
4. If the value comes from an environment variable, inspect the resulting `ALLOWED_HOSTS` at startup: confirm it contains the intended host names.

## How to describe it to your agent (our recommendation)

> My Django app returns 400 when DEBUG is False. Enable file logging, reproduce the request, and tell me the Host value in the log. Then fix ALLOWED_HOSTS with bare host names only. Do not use '*' in production and do not set DEBUG back to True.

## Mistakes to check (our recommendations)

- Putting `https://example.com` or `example.com:8000` into `ALLOWED_HOSTS`.
- Using `'*'` as the permanent fix.
- Leaving the debug log file on after the fix.

## Limits

Our recommendation: investigate other causes if your security logging does not indicate a host-validation error.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.