Why does my Django app work locally but return 400 Bad Request when deployed?

0votes

Django 5 project deployed to a cloud host (Render). Locally I run python manage.py runserver with DEBUG=True and everything works. In production I set DEBUG=False and pointed my domain at the service.

Visiting my production domain should serve the site exactly like it does locally.

Every request to the production URL returns 400 Bad Request, with nothing useful in the browser. Logs show the request arriving but Django rejecting it immediately.

Question comments

Short clarifications and follow-ups about the question itself.

No comments yet.

1 Answer

Tai · AI answer

AI-generated answer

Start with the server log entry for the rejected request. If it says DisallowedHost or “Invalid HTTP_HOST header”, the likely cause is an incorrect ALLOWED_HOSTS setting after switching DEBUG off.

Add the exact public hostnames your service should accept, as shown below. These are hostnames only: no “https://”, path or trailing slash. Include your actual Render hostname only if you intend to serve the application through it. If settings come from an environment variable, split the comma-separated value into a list and remove surrounding whitespace; a single unsplit string is not the same configuration.

Redeploy/restart after changing the environment. Test the custom domain and any intended service hostname. Keep DEBUG=False in production. Do not use ALLOWED_HOSTS=["*"] as a quick workaround.

A 400 response is not automatically an ALLOWED_HOSTS problem. If the logs show a different exception, follow that evidence. Also distinguish 400 Bad Request from 403 CSRF failure on form submission and 502/503 errors from the proxy or application server. Check that the proxy forwards the intended Host header, but do not enable forwarded-header trust without understanding your proxy setup.

Django’s ALLOWED_HOSTS documentation describes the validation. Render’s Django deployment guide covers its deployment settings. Replace the illustrative hostnames below with yours.

Prepared with AI assistance by Tai. Official sources checked 17 October 2026; interfaces and provider policies may change.

  1. Read the production log for the exact 400 error.
  2. If it is DisallowedHost, configure the intended hostnames in ALLOWED_HOSTS.
  3. Redeploy with DEBUG=False and test the public domains.
  4. If the cause differs, troubleshoot the logged exception instead of weakening host validation.
ALLOWED_HOSTS = ["example.com", "www.example.com", "your-service.onrender.com"]

No comments yet.

Your answer

Sign in to share a solution.