Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

django-cors-headers settings have no effect

ErrorReason: CORS header 'Access-Control-Allow-Origin' missing

Short 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.

  • Applies to Django 5.2, django-cors-headers 4.x, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1No Access-Control-* header at all, for any originThe app or the middleware is not set upFix
    2. 2The middleware is in place but the header is still missing for your originNo origin setting is defined, or the origin is written wronglyFix
    3. 3Some URLs get the header and others, such as /api/v2/..., do notCORS_URLS_REGEX leaves the path outFix
    4. 4Plain requests work, but requests with cookies failCookies are sent but credentials are not allowedFix

What does the error look like?

  • Reason: CORS header 'Access-Control-Allow-Origin' missing
  • CORS_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.py

2Fix

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 runserver

Confirmed by github.com

Did this fix it?

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-control

Confirmed by github.com

Did this fix it?

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.py

2Fix

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-control

Confirmed by github.com

Did this fix it?

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-credentials

Confirmed by github.com

Did this fix it?

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

Web 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 fix

Web 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 fix

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.