django-cors-headers is a Django app that adds CORS response headers so browser clients on other origins can call your Django API. You configure allowed origins or regexes in Django settings.
Overview
django-cors-headers is a Django package that implements Cross-Origin Resource Sharing by attaching the needed headers to HTTP responses. It is intended for cases where front-end apps on different domains need to access Django-backed resources in the browser.
You install it from pip, add corsheaders to INSTALLED_APPS, and place CorsMiddleware high in the middleware stack, before middleware that may return responses without reaching CORS handling. Documentation stresses understanding CORS implications before enabling broad access.
Configuration centers on CORS_ALLOWED_ORIGINS, CORS_ALLOWED_ORIGIN_REGEXES, or CORS_ALLOW_ALL_ORIGINS, plus optional controls for methods, headers, credentials, URL regex scoping, and preflight cache duration. Signals allow custom logic to approve requests, and CSRF_TRUSTED_ORIGINS should align when using cookies cross-origin.
The package supports recent Django and Python versions as documented in its requirements section.
Key features
- CORS headers for Django responses
- Origin allow lists and regex rules
- Configurable methods and headers
- Credential and preflight options
- check_request_enabled signal hook
- CSRF integration guidance
Use cases
- Browser SPAs calling a Django API
- Controlled cross-origin API access
- Selective CORS on URL patterns