1、安装
pip install django-cors-headers
2、settings配置
INSTALLED_APPS = [
...
'corsheaders', # 跨域配置
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware', # 跨域设置,放最前面
...
]
'''
跨域设置
'''
CORS_ALLOW_CREDENTIALS = True # 允许携带cookie
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = () # 跨域白名单
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
)