How to Fix CORS Errors Between React and Django
- Author
- Vishal Maurya
- Published on
- Reading time
- 2 min read
Overview
A CORS error usually means the browser did not accept a cross-origin response. It does not necessarily mean Django is down. The correct fix depends on the request origin, whether the browser sends credentials, and whether the request triggers a preflight.
First inspect the browser Network panel. Identify the Origin, request method, response headers, and whether an OPTIONS preflight failed. Then configure the exact frontend origins needed for the environment.
Debug in this order
- Confirm the frontend origin includes scheme, hostname, and port.
- Confirm Django returns the expected
Access-Control-Allow-Originheader. - Check preflight responses for allowed methods and headers.
- If using cookies, configure credentials on both sides and use a specific allowed origin rather than
*. - For session-authenticated unsafe methods, configure CSRF trusted origins and send the CSRF token as required.
- Repeat the test in local, preview, and production environments.
Example: a concrete starting point
# settings.py — example using django-cors-headers
INSTALLED_APPS = [
# ...
"corsheaders",
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
# keep it before middleware that may generate responses
"django.middleware.common.CommonMiddleware",
# ...
]
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000",
"https://app.example.com",
]
The example illustrates the boundary and validation pattern; adapt names, persistence, error handling, and provider-specific details to your application. It is not a complete production system by itself.
Failure cases to test
Do not “fix” CORS by allowing every origin with credentials or by disabling browser security. CORS does not replace authentication, and command-line clients may call the API regardless of browser CORS rules.
Before release, test the happy path, invalid input, missing permissions, dependency failure, and a retry or duplicate request where relevant. Record expected behavior so the implementation can be reviewed against the real business requirement.
When to bring in a developer
If CORS, cookies, or CSRF behaves differently between local and production, I can trace the request path and configure the frontend and Django API together.
Conclusion
The reliable version of this feature is not just the happy-path demo. It makes ownership, validation, authorization, failure handling, and operational behavior explicit. Start with the smallest useful workflow, measure it against real data, and expand only when the next requirement is clear.