Django通过中间件配置解决跨域

一、通过 django-cors-headers 实现
  1. pip install django-cors-headers

  2. 配置settings.py文件
    在INSTALLED_APPS里添加“corsheaders”
    INSTALLED_APPS = [
    ...
    'corsheaders']

  3. 在settiongs 里 MIDDLEWARE 中添加如下
    MIDDLEWARE = [
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',]

  4. 最后在 settings.py 末尾添加

#跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
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',
)

猜你喜欢

转载自www.cnblogs.com/shiqi17/p/12344991.html
今日推荐