文章目录
- 安装库
pip install django-cors-headers
- 配置
# settings.py
# 配置
INSTALLED_APPS = [
'corsheaders',
]
# 添加中间件
MIDDLEWARE_CLASSES = (
# 在CommonMiddleware的上方添加下面这行
'corsheaders.middleware.CorsMiddleware',
)
# 添加白名单
# 允许携带cookie,默认为False时携带token
CORS_ALLOW_CREDENTIALS = True
## 所有前端域名或ip
CORS_ORIGIN_ALLOW_ALL = True
## 指定的前端域名或ip以及对应的后端域名
# CORS_ORIGIN_WHITELIST = [
# "http://127.0.0.1:8080",
# "http://localhost:8080"
# "http://127.0.0.1:8000",
# "http://localhost:8000"
# ]