Cross-Origin Resource Sharing (CORS) is a mechanism that allows a web application running at one origin, accessible from a different origin. This is helpful when you want an API accessible from all domains.
You can enable CORS in a django application in simple steps. What you need to do is just:
- Install django-cors-headers
pip install django-cors-headers
- Add ‘corsheaders’ in INSTALLED_APPS in settings.py
INSTALLED_APPS = [ ... 'corsheaders', ... ]
- Add ‘CorsMiddleware’ at the top of middleware in settings.py
MIDDLEWARE = [ ... 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ]
- CORS_ORIGIN_ALLOW_ALL = True
See Image below for your reference:
Hope, It helps you.