标签:bugs chain class hosts pps djang ref pre github
Django-cors-headers
解决 django 开发中遇到的跨域访问资源问题
首先安装Django-cors-headers
扩展类
# 安装 django-cors-headers 解决跨域问题:
pip install django-cors-headers
INSTALLED_APPS = (
...
# 添加 django-cors-headers 使其可以进行 cors 跨域
‘corsheaders‘,
...
)
MIDDLEWARE = [
...
# 添加 django-cors-headers 使其可以进行 cors 跨域
‘corsheaders.middleware.CorsMiddleware‘,
]
在settings.py文件中添加:
# CORS跨域请求白名单设置
CORS_ORIGIN_WHITELIST = (
‘http://127.0.0.1:8080‘,
‘http://localhost:8080‘,
‘http://www.linubugs.site:8080‘,
)
CORS_ALLOW_CREDENTIALS = True # 允许携带cookie
CORS_ALLOW_CREDENTIALS
指明在跨域访问中后端是否支持cookie
的操作.给 settings.py 中的 ALLOWED_HOSTS 添加数据:
ALLOWED_HOSTS = [‘api.linuxbugs.site‘,
‘127.0.0.1‘,
‘localhost‘,
‘www.linuxbugs.site‘]
标签:bugs chain class hosts pps djang ref pre github
原文地址:https://www.cnblogs.com/duzhaoqi/p/13262470.html