drf解决跨域问题 使用 django-corse-headers扩展

Posted a-way-blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了drf解决跨域问题 使用 django-corse-headers扩展相关的知识,希望对你有一定的参考价值。

跨域CORS

使用django-corse-headers扩展

安装

pip install django-cors-headers

添加应用

INSTALLED_APPS = (
    ...
    ‘corsheaders‘,
    ...
)

中间层设置

MIDDLEWARE = [
    ‘corsheaders.middleware.CorsMiddleware‘,
    ...
]

添加白名单

# CORS
CORS_ORIGIN_WHITELIST = (
    ‘127.0.0.1:8080‘,
    ‘localhost:8080‘,
)
CORS_ALLOW_CREDENTIALS = True  # 允许携带cookie

以上是关于drf解决跨域问题 使用 django-corse-headers扩展的主要内容,如果未能解决你的问题,请参考以下文章