如何在我的 Django REST 框架上启用 CORS? reference没有太大帮助,它说我可以通过中间件来做,但是我该怎么做呢?
最佳答案
您在问题中引用的链接建议使用 django-cors-headers
,其documentation说要安装库
python -m pip install django-cors-headers
然后将其添加到您安装的应用程序中:
INSTALLED_APPS = (
...
'corsheaders',
...
)
您还需要添加一个中间件类来监听响应:
MIDDLEWARE = [
...,
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...,
]
并为 CORS 指定域,例如:
CORS_ALLOWED_ORIGINS = [
'http://localhost:3030',
]
请浏览the configuration section其文档,特别注意各种 CORS_ORIGIN_
设置。您需要根据自己的需要设置其中的一些。
关于python - 如何在 Django REST 框架上启用 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35760943/