码迷,mamicode.com
首页 > 其他好文 > 详细

DRF跨域问题

时间:2019-04-06 22:57:37      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:安装   res   ram   mes   click   cat   esc   pos   can   

一、安装模块

 pip install django-cors-headers

django-cors-headers 使用说明:https://github.com/ottoyiu/django-cors-headers

二、添加到INSTALL_APPS中

INSTALLED_APPS = (
    ...
    coreschema,
 ... )

三、添加中间件

下面添加中间件的说明:

CorsMiddleware should be placed as high as possible, especially before any middleware that can generate responses such as Django‘s CommonMiddleware or Whitenoise‘s WhiteNoiseMiddleware. If it is not before, it will not be able to add the CORS headers to these responses.

Also if you are using CORS_REPLACE_HTTPS_REFERER it should be placed before Django‘s CsrfViewMiddleware (see more below).

意思就是 要放的尽可能靠前,必须在CsrfViewMiddleware之前。我们直接放在第一个位置就好了

MIDDLEWARE = [
    ‘corsheaders.middleware.CorsMiddleware‘,
    django.middleware.security.SecurityMiddleware,
    django.contrib.sessions.middleware.SessionMiddleware,
    django.middleware.common.CommonMiddleware,
    django.middleware.csrf.CsrfViewMiddleware,
    django.contrib.auth.middleware.AuthenticationMiddleware,
    django.contrib.messages.middleware.MessageMiddleware,
    django.middleware.clickjacking.XFrameOptionsMiddleware,
]

四、设置为True

CORS_ORIGIN_ALLOW_ALL = True

 

DRF跨域问题

标签:安装   res   ram   mes   click   cat   esc   pos   can   

原文地址:https://www.cnblogs.com/mxsf/p/10663077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!