Categories
Python Answers

How to enable CORS on Python Django REST Framework?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *