Access-Control-Allow-Origin in Django app

Firstly we have install the "django-cors-headers".

Install it using pip:

pip install django-cors-headers

Add 'corsheaders' to your installed apps:


 Add middleware:

MIDDLEWARE = [

    'corsheaders.middleware.CorsMiddleware',

    'django.middleware.common.CommonMiddleware',

  ]

Then add this to your "settings.py":

ALLOWED_HOSTS = ['*']

CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_CREDENTIALS = True

CORS_ALLOW_HEADERS = ['*']

If you also want to allow some domains to make "POST" requests, add this to your "settings.py" and don't forget to add it in "CORS_ALLOWED_ORIGINS".

CSRF_TRUSTED_ORIGINS = [

        'http://localhost:4200',

  ]

I hope It works.

Sign In or Register to comment.