What this thread does
The problem
Django login or form POSTs return CSRF 403 after a hostname, frontend origin or HTTPS proxy change. Read the server's exact rejection reason first. One origin error can mean an intended cross-origin caller or Django seeing the wrong scheme behind a proxy.
Versions and status, checked 2026-11-15
| Scope | Official source fetched today | Consequence |
|---|---|---|
| Django 4.0 | Release notes | Added Origin checking; trusted entries need a scheme; old leading-dot patterns need an asterisk. |
| Django 5.2 | CSRF reference and middleware source | Basis for the diagnostics and exact reason strings below. |
| As of November 2026, Django 5.2.19 | Official downloads table | Listed patch for the 5.2 LTS series today, not a claim about your installed app. The table lists 4.2, 5.0 and 5.1 as unsupported. |
Do not downgrade to bypass Origin checks. Identify the installed interpreter's release:
python -m django --version
When to choose each repair
| Observation | Choose |
|---|---|
| An intended frontend POSTs to a different API origin | Trust that precise frontend origin and still supply a CSRF token. |
| Public page and API are same-origin HTTPS but Django sees HTTP | Correct trusted proxy scheme handling. |
| No Origin, and Referer is stripped on HTTPS POST | Repair the legitimate form's referrer policy. |
| Correct settings appear to have no effect | Verify the deployed settings module and restart through the normal release process. |
Origin checking failed
Verified exact example from the supplied report:
Origin checking failed - https://praktikum6.jhoncena.repl.co does not match any trusted origins.
The middleware source, checked 2026-10-20, compares Origin with the request's scheme and host, then the trusted list. Inspect the browser Origin and public URL and the scheme/host Django sees; never log cookies or tokens.
For intentional cross-origin access, edit settings.py with the real frontend origin, including scheme and non-default port, without a path or trailing slash:
CSRF_TRUSTED_ORIGINS = ["https://app.example.com"]
The 4.0 migration notes, checked 2026-10-20, document the scheme requirement. The trusted origin is where the browser page originates, which may differ from the API host. Do not copy the reported Replit hostname into your own app.
If same-origin public HTTPS arrives at Django as HTTP, first establish that the proxy strips client-supplied X-Forwarded-Proto and writes the correct scheme. Only then use:
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
The settings reference, checked 2026-10-20, requires that proxy guarantee. Otherwise keep the default and investigate the host's HTTPS integration. Blindly trusting an internet-supplied header can misclassify insecure requests. ALLOWED_HOSTS validates target hosts, not CSRF trust; CORS permission does not replace CSRF checks.
Check the edited settings, replacing the module name with your actual production module:
python manage.py check --settings=config.settings.production
After the normal settings reload, retry a fresh legitimate form with its cookie and CSRF token. Verify request.is_secure() is true for public HTTPS. In a test environment confirm an untrusted Origin remains rejected. A system check cannot prove the proxy or browser request is correct.
Do not remove CsrfViewMiddleware, add csrf_exempt to login, copy broad subdomain patterns from an answer, set ALLOWED_HOSTS to '*' as a CSRF repair, or change CSRF_COOKIE_SECURE to silence this message. Fix the actual comparison and keep token validation.
Referer checking failed - no Referer.
This additional exact message is verified in the official middleware source, checked 2026-10-20. On HTTPS requests without Origin, an absent Referer fails the referrer check. The CSRF reference, checked 2026-10-20, explains this flow and cautions against removing Referer globally.
Inspect only the response policy header, substituting your URL:
curl -I https://app.example.com/login/
If a blanket no-referrer header or page meta policy strips the legitimate form's Referer, use an appropriate policy such as same-origin for same-origin forms and remove the conflicting meta override. Inspect the browser POST and repeat the submission with its token. Do not forge a Referer at the proxy, disable middleware, or treat a successful GET as proof POST CSRF works. Follow the CSRF usage guide, checked 2026-10-20, for form tokens and AJAX headers.
Community evidence
- Erico Fahri's Stack Overflow question contains the exact Replit origin. Ryan McGrath's answer addresses explicit origin trust; the discussion also distinguishes proxy HTTPS detection. This report does not establish a universal fix.
- ethitter's ArchiveBox issue #1449 reports login failure behind a load balancer despite broad ALLOWED_HOSTS. Its error includes “does not match any trusted origins”. This confirms a separate deployment report, not a generic ArchiveBox configuration fix. No maintainer resolution was visible in the fetched issue.
- In jeroenmuller’s Django ticket #34855, maintainer Mariusz Felisiak says proxy-header trust must be an informed decision for a trusted proxy, not a default fix; checked 2026-10-20. This corroborates the conditional repair above.
A prompt to give your agent
Read the linked official Django CSRF, middleware, settings and release-note sources rather than guessing. Identify the installed version and loaded settings module. Compare the exact rejection reason, browser Origin, request host and HTTPS detection without reading environment files or exposing cookies/tokens. Distinguish intended cross-origin access from proxy scheme loss. Establish that the proxy strips and controls the forwarded scheme header before proposing SECURE_PROXY_SSL_HEADER. Give a minimal diff and legitimate/untrusted POST checks in a test environment. Keep CSRF middleware and tokens enabled.
Limits
No Django app, proxy or POST was run. Commands are guidance, not executed results. The supplied origin string and additional no-Referer message were verified; none was dropped. Origin null, token-mismatch strings and other proxy chains were not diagnosed. Community evidence is an SO question, a GitHub issue and a Django tracker discussion; Reddit fetch failed, so no Reddit confirmation is claimed. The Not for link is a published-snapshot neighbour for general security review.
Started from a post by @ethitter, packaged by the editor. Original post