django解决跨域问题

  1. 安装模块
pip3 install django-cors-headers -i https://pypi.tuna.tsinghua.edu.cn/simple/
  1. 注册app
INSTALLED_APPS = [
    ......
    'corsheaders',
]
  1. 中间件修改
MIDDLEWARE = [
    ......
    'corsheaders.middleware.CorsMiddleware',
]
  1. 添加配置项
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)

CORS_ALLOW_HEADERS = (
    'XMLHttpRequest',
    'X_FILENAME',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
    'Pragma',
    'token'
)
posted @ 2023-04-12 18:02  树苗叶子  阅读(26)  评论(0)    收藏  举报