[Django_Rest_Framework] Enable CORS

Rex Chiang
Dec 16, 2021

--

CORS (Cross-Origin Resource Sharing)

When we develop Django REST API would need to interact with other applications that hosted on different domains. For these requests, we should enable CORS in our server.

Install required package

pip3 install django-cors-headers

Update installed apps in settings.py

INSTALLED_APPS = [
... ,
'corsheaders',
]

Update middleware in settings.py

MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
... ,
]

Enable CORS in settings.py

CORS_ORIGIN_ALLOW_ALL = True

--

--

No responses yet