Django CORS Headers: Configure Origins, Sessions and CSRF
By Ankit Jain · Updated
Place django-cors-headers correctly and distinguish CORS_ALLOWED_ORIGINS, CSRF_TRUSTED_ORIGINS and cookie settings.
django-cors-headers adds CORS response permission. Django’s CSRF middleware separately protects state-changing requests that use browser sessions. Configuring one does not automatically configure the other.
This guide uses Django 5.2-style settings and the current django-cors-headers configuration names. Use a package release that supports your installed Django and Python versions.
Install and place the middleware
python -m pip install django-cors-headers
Merge these entries into settings.py; keep the rest of your application settings:
INSTALLED_APPS = [
# ... existing applications ...
"corsheaders",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"corsheaders.middleware.CorsMiddleware",
# Place CorsMiddleware above middleware that can generate responses below.
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
# ... existing middleware ...
]
CORS_ALLOWED_ORIGINS = ["https://app.example.com", "http://localhost:5173"]
CORS_URLS_REGEX = r"^/api/.*$"
Middleware ordering affects error responses. If a layer above CorsMiddleware produces a response, inspect and configure that layer too. Do not duplicate CORS headers in Nginx or a CDN.
For a cookie-session frontend
CORS_ALLOW_CREDENTIALS = True
CSRF_TRUSTED_ORIGINS = ["https://app.example.com"]
Use credentials: include on the frontend. For state-changing session requests, obtain a valid CSRF token through your application’s designed flow and send it with the request, commonly as X-CSRFToken. A trusted origin is not permission to omit the CSRF token.
CSRF_TRUSTED_ORIGINS configures Django’s CSRF origin trust. CORS_ALLOWED_ORIGINS configures browser response permission. ALLOWED_HOSTS validates hosts used to reach Django. These lists have different jobs.
If the deployment is genuinely cross-site and needs cross-site cookies, consider SESSION_COOKIE_SAMESITE = “None”, CSRF_COOKIE_SAMESITE = “None”, and Secure cookie settings over HTTPS. Do not apply those settings automatically to every cross-origin deployment; subdomains can be same-site, and browser third-party cookie restrictions may still prevent inclusion.
Token APIs and DRF
An Authorization header requires CORS preflight permission and actual token validation. Review your Django REST framework authentication classes: SessionAuthentication introduces CSRF requirements for authenticated session requests. Do not broadly disable CSRF to solve a CORS message.
Diagnose Invalid CORS request and 403 responses
Identify whether the response came from Django, a proxy, or another service. The wording alone does not identify the failing configuration. If OPTIONS succeeds but the actual POST returns 403, inspect the CSRF failure details and authentication separately. A readable 403 is not itself a CORS failure.
Verify approved origins, preflight, a controlled unauthorized request, a missing /api/ route, and CSRF-protected writes from your actual frontend. See credentials, CSRF, and error response permission.
Use the DevTools debugging workflow to inspect the frontend request, then analyze captured headers in the CORS Tester.