Django CORS配置方案

参考

https://pypi.org/project/django-cors-headers/

在setting.py中设置

python 复制代码
INSTALLED_APPS = [
	......
	'corsheaders', #添加此行
]

MIDDLEWARE=[
	......
	'corsheaders.middleware.CorsMiddleware', #添加此行
	'django.middleware.common.CommonMiddleware',
	#'django.middleware.csrf.CsrfViewMiddleware',#注释此行
	......
]

#添加配置此白名单
#CORS_ALLOW_ALL_ORIGINS =TRUE
CORS_ALLOWED_ORIGINS = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000",
]
CORS_ALLOW_METHODS = (
    "DELETE",
    "GET",
    "OPTIONS",
    "PATCH",
    "POST",
    "PUT",
)

CORS_ALLOW_HEADERS = (
    "accept",
    "authorization",
    "content-type",
    "user-agent",
    "x-csrftoken",
    "x-requested-with",
)
相关推荐
Warson_L14 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅15 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅15 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L15 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅15 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L16 小时前
python的类&继承
python
Warson_L16 小时前
类型标注/type annotation
python
ThreeS18 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵20 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏