import error django corsheaders
Using pip
:
pip install django-cors-headers
Using pipenv
:
pipenv install django-cors-headers
Try This.
pip install --user django-cors-headers
Step 1. Install corsheaders:
python -m pip install django-cors-headers
Step 2. Add cors-headers to INSTALLED_APPS:
INSTALLED_APPS = (
...
'corsheaders',
...
)
Step 3. Add the Cors headers middleware:
MIDDLEWARE = [
...,
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...,
]
NOTE: Bear in mind that you should place the CorsMiddleware
as high as possible to not be overwriten by other middleware or any behaviour caused by other middleware.
Step 4. Add cors headers in settings.py
:
CORS_ALLOWED_ORIGINS = [
'http://127.0.0.1:3000',
'http://localhost:3030',
'yoursite.com'
]
Note: Also check whether the endpoint you are requesting are within the CORS_ALLOWED_ORIGINS
. Also, avoid using '*'
within the same variable to avoid any security breach.
Please, check its documentation there are way more detail and links that can solve some questions you have:
https://github.com/adamchainz/django-cors-headers#setup
I had the same problem after I installed via pip. Then I downloaded source and manually installed the django-cors-headers
after that the problem was gone.