To enable CORS on Python Django REST Framework, we add the django-cors-headers
package.
To install it, we run
python -m pip install django-cors-headers
Then we add
INSTALLED_APPS = (
...
'corsheaders',
...
)
in INSTALLED_APPS
.
And add
MIDDLEWARE = [
...,
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...,
]
in middleware.
Then we add
CORS_ALLOWED_ORIGINS = [
'http://localhost:3030',
]
in our Django’s config to allow CORS in the hosts listed.