1. 在setting.py的INSTALLED_APPS中配置如下
INSTALLED_APPS = [ 'corsheaders', ]
2 在setting.py的MIDDLEWARE中配置如下
注释掉csrf并添加'corsheaders.middleware.CorsMiddleware'
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'corsheaders.middleware.CorsMiddleware', #跨域 'django.middleware.common.CommonMiddleware', # 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
3 settings.py
CORS_ORIGIN_ALLOW_ALL = True