django-cors-headers is a Django application that simplifies the handling of server headers required for Cross-Origin Resource Sharing (CORS). It provides a robust and flexible solution to manage cross-origin requests, allowing Django applications to securely interact with frontend applications hosted on different domains. The current version is 4.9.0, and it maintains an active release cadence with regular updates and community support.
pip install django-cors-headersVerified import paths — ran on the pinned version, not inferred.
To quickly enable CORS, install the package, then add `corsheaders` to your `INSTALLED_APPS` and `CorsMiddleware` to the top of your `MIDDLEWARE` list in `settings.py`. Finally, configure `CORS_ALLOWED_ORIGINS` with a list of allowed frontend domains. For development, `CORS_ALLOW_ALL_ORIGINS = True` can be used temporarily but is not recommended for production due to security risks. Remember that middleware order is crucial for correct functionality.
Ensure 'corsheaders.middleware.CorsMiddleware' is the first entry in your MIDDLEWARE setting in settings.py.
Update your settings to use `CORS_ALLOWED_ORIGINS` instead of `CORS_ORIGIN_WHITELIST`, and ensure all origins include their full URI scheme (e.g., 'http://localhost:3000').
In production, always use `CORS_ALLOWED_ORIGINS` to specify a list of trusted origins. For dynamic origins, consider `CORS_ALLOWED_ORIGIN_REGEXES`.
Set `CORS_ALLOW_CREDENTIALS = True` in `settings.py` and configure your frontend to send credentials. Adjust `SESSION_COOKIE_SAMESITE = 'None'` if necessary for session cookies to be sent cross-domain.
Configure CORS policies directly on your CDN, cloud storage bucket, or web server (Nginx/Apache) for assets served from those locations.
Remove `CORS_REPLACE_HTTPS_REFERER` and `CorsPostCsrfMiddleware`. Configure `CSRF_TRUSTED_ORIGINS` in your `settings.py` with your trusted frontend origins.
Ensure each middleware class in the MIDDLEWARE setting is separated by a comma.
Install and configure django-cors-headers, add 'corsheaders.middleware.CorsMiddleware' to MIDDLEWARE, and set CORS_ALLOWED_ORIGINS or CORS_ALLOW_ALL_ORIGINS in settings.py.
Upgrade django-cors-headers to version 3.10.1 or later to ensure compatibility with Django 4.0 and above.
Install django-cors-headers, add 'corsheaders.middleware.CorsMiddleware' to MIDDLEWARE, and configure CORS_ALLOWED_ORIGINS or CORS_ALLOW_ALL_ORIGINS in settings.py.
Install the package using pip: pip install django-cors-headers.
No dependency data recorded yet.