개발일기/파이썬(Python)
django 크로스 도메인 설정
프로그래머콩
2019. 3. 31. 04:34

django 크로스도메인 설정
$pip install django-cors-headers
< settings.py >
INSTALLED_APPS 에
'corsheaders' 추가
MIDDLEWARE에
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsMiddleware' 추가
맨 마지막에
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
'도메인',
)
CORS_ORIGIN_REGEX_WHITELIST = (
'도메인',
)
그리고
views.py에서 소스 리턴시 아래와 같은 코드 추가 입력
# Cross Domain 설정
response = HttpResponse('success')
response["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
response["Access-Control-Allow-Origin"] = "*"
response["Acess-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "X-Requested-With, Content-Type"
return response