如何在Django REST Framework上启用CORS [英] How can I enable CORS on Django REST Framework

查看:391
本文介绍了如何在Django REST Framework上启用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的Django REST框架上启用CORS?
参考无用,

How can I enable CORS on my Django REST Framework? the reference doesn't help much, it says that I can do by a middleware, but how can I do that?

推荐答案

您在问题中引用的链接建议使用 django-cors-headers ,其中文档说要安装库

The link you referenced in your question recommends using django-cors-headers, whose documentation says to install the library


pip install django-cors-headers

,然后将其添加到您安装的应用程序中:

and then add it to your installed apps:

INSTALLED_APPS = (
    ...
    'corsheaders',
    ...
)

您还需要添加一个中间件类来监听响应:

You will also need to add a middleware class to listen in on responses:

MIDDLEWARE_CLASSES = (
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
)


您可能还需要浏览其文档的配置部分

这篇关于如何在Django REST Framework上启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆