vue 与django 前后端分离解决跨域问题

安装
pip install django-cors-headers

djagno settings

添加

# 跨域访问
CORS_ORIGIN_ALLOW_ALL = True
然后settins中INSTALLED_APPS配置和settings中MIDDLEWARE配置
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_rabbitmq.apps.DjangoRabbitmqConfig',
    'rest_framework',
    'rest_framework_swagger',
    'corsheaders',
]
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',
]注意顺序

你可能感兴趣的:(vue 与django 前后端分离解决跨域问题)