django-cors-headers settings have no effect
Reason: CORS header 'Access-Control-Allow-Origin' missingShort answer
django-cors-headers only works when corsheaders is in INSTALLED_APPS, CorsMiddleware sits high in MIDDLEWARE (before CommonMiddleware), and at least one origin setting is defined. Each origin needs a scheme and host, such as https://example.com. CORS_URLS_REGEX can also limit which paths get the headers.
Fix it fast
1Find out which cause you have
Send a request with an Origin header straight to the Django view and look at the response headers. This takes the browser out of the picture.
curl -s -D - -o /dev/null \ -H "Origin: https://app.example.com" \ http://127.0.0.1:8000/api/items/2Match what you see, then jump to its fix
- 1
No Access-Control-* header at all, for any originThe app or the middleware is not set upFix - 2
The middleware is in place but the header is still missing for your originNo origin setting is defined, or the origin is written wronglyFix - 3
Some URLs get the header and others, such as /api/v2/..., do notCORS_URLS_REGEX leaves the path outFix - 4
Plain requests work, but requests with cookies failCookies are sent but credentials are not allowedFix
- 1
What does the error look like?
Reason: CORS header 'Access-Control-Allow-Origin' missingCORS_ALLOWED_ORIGINS not working
Cause 1 of 4
The app or the middleware is not set up
1Check
Open settings.py. corsheaders must be in INSTALLED_APPS, with the trailing comma, and CorsMiddleware must be in MIDDLEWARE.
grep -n -E "corsheaders|CorsMiddleware" settings.py2Fix
Install the package, add the app and put the middleware as high as possible, before django.middleware.common.CommonMiddleware.
# pip install django-cors-headers
INSTALLED_APPS = [
# ...
"corsheaders",
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
# ...
]3Verify
Restart the server and repeat the curl from the first step.
python manage.py runserverConfirmed by github.com
Cause 2 of 4
No origin setting is defined, or the origin is written wrongly
1Check
At least one of CORS_ALLOWED_ORIGINS, CORS_ALLOWED_ORIGIN_REGEXES or CORS_ALLOW_ALL_ORIGINS must be set. An origin is scheme, host and port (the port may be left out when it is the default), for example https://example.com or http://localhost:8080.
python manage.py shell -c "from django.conf import settings; print(settings.CORS_ALLOWED_ORIGINS)"2Fix
List each origin with its scheme. Use CORS_ALLOWED_ORIGIN_REGEXES for many subdomains. CORS_ALLOW_ALL_ORIGINS = True overrides the other origin settings and lets any site call your API, so keep it to local development.
CORS_ALLOWED_ORIGINS = [
"https://app.example.com",
"http://localhost:3000",
]
# or, for many subdomains
CORS_ALLOWED_ORIGIN_REGEXES = [r"^https://\w+\.example\.com$"]3Verify
Repeat the curl with the exact origin you listed.
curl -s -D - -o /dev/null -H "Origin: https://app.example.com" http://127.0.0.1:8000/api/items/ | grep -i access-controlConfirmed by github.com
Cause 3 of 4
CORS_URLS_REGEX leaves the path out
1Check
CORS_URLS_REGEX limits which request paths get CORS headers. The default matches every path, so a value set earlier in the project may be too narrow.
grep -n CORS_URLS_REGEX settings.py2Fix
Make the regular expression cover every path the front end calls.
CORS_URLS_REGEX = r"^/api/.*$"3Verify
Request one path from each area of the API and check each response.
curl -s -D - -o /dev/null -H "Origin: https://app.example.com" http://127.0.0.1:8000/api/v2/items/ | grep -i access-controlConfirmed by github.com
Cause 4 of 4
Cookies are sent but credentials are not allowed
1Check
Cross-site cookies need CORS_ALLOW_CREDENTIALS = True. Django's SESSION_COOKIE_SAMESITE is Lax by default, and that blocks the session cookie across sites.
python manage.py shell -c "from django.conf import settings; print(settings.CORS_ALLOW_CREDENTIALS, settings.SESSION_COOKIE_SAMESITE)"2Fix
Set CORS_ALLOW_CREDENTIALS = True with an explicit origin list, never a wildcard. Changing SESSION_COOKIE_SAMESITE to None weakens a security restriction, so do it only if the cross-site cookie is really needed.
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = ["https://app.example.com"]3Verify
Check that the response carries Access-Control-Allow-Credentials: true.
curl -s -D - -o /dev/null -H "Origin: https://app.example.com" http://127.0.0.1:8000/api/items/ | grep -i allow-credentialsConfirmed by github.com
How do I stop it coming back?
- Keep CorsMiddleware at the top of MIDDLEWARE and leave a comment saying why.
- Use a separate settings file for development so CORS_ALLOW_ALL_ORIGINS never reaches production.
- Add a test that requests the API with an Origin header and asserts the allow-origin header.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Django cannot import corsheaders (django-cors-headers)
ImportError: No module named corsheaders
The pip package is django-cors-headers, but the module Django imports is corsheaders.
3 causes, each with a fixWeb apps
CORS fails when you open an HTML file straight from disk
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///
A page opened from disk has an opaque origin, so browsers treat every request it makes as cross-origin, and CORS only works over http or https.
3 causes, each with a fixWeb apps
Apollo Client is blocked by CORS when the GraphQL server runs on AWS Lambda
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apollo Server does not set CORS for you when it runs behind a framework or integration, and on Lambda that layer is API Gateway.
5 causes, each with a fixWeb apps
A POST to a Google Apps Script web app returns 405 or a CORS error
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.
3 causes, each with a fixWeb apps
Axios sends no cookie to an Express API even with withCredentials set to true
The login response has a Set-Cookie header, but the browser stores nothing
withCredentials only tells the browser to attach cookies.
4 causes, each with a fixWeb apps
A Chrome extension content script cannot read another site's response
Access to fetch at 'https://api.example.com/price' from origin 'https://shop.example.net' has been blocked by CORS policy
Since Chrome 85 a content script's request is treated as a request from the web page it runs in, so the page's CORS rules apply and the extension's host permissions do not help.
3 causes, each with a fix