cors headers in django code example
Example 1: django-cors-headers
$ pipenv install django-cors-headers
# or
$ pip install django-cors-headers
Example 2: install crossheaders in django
pip install django-cors-headers
#Add corsheaders to installed applications in settings.py:
INSTALLED_APPS = [
...
'corsheaders',
]
#Add corsheaders.middleware.CorsMiddleware to middleware section in settings.py:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
#Allow access to all domains by setting the following variable to TRUE in settings.py:
DEBUG = True
ALLOWED_HOSTS = []
CORS_ORIGIN_ALLOW_ALL = True
Example 3: django-cors-headers
MIDDLEWARE = [
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
Example 4: django-cors-headers
python -m pip install django-cors-headers
Example 5: django-cors-headers
INSTALLED_APPS = [
...
'corsheaders',
...
]