Enable CORS in django

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:

  1. Install django-cors-headers
    pip install django-cors-headers
  2. Add ‘corsheaders’ in INSTALLED_APPS in settings.py

    INSTALLED_APPS = [
        ...
        'corsheaders',
        ...
    ]
  3. Add ‘CorsMiddleware’ at the top of middleware in settings.py

    MIDDLEWARE =
        ...
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.common.CommonMiddleware',
        ...
    ]
  4. CORS_ORIGIN_ALLOW_ALL = True

See Image below for your reference:

cors-in-django

Hope, It helps you.

Leave a Reply

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

Back to Top
Shares