INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘corsheaders’,
‘rest_framework’,
‘users.apps.UsersConfig’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
]
CORS_ORIGIN_WHITELIST = (
‘http://127.0.0.1:8080’,
‘http://localhost:8080’,
‘http://www.meiduo.site:8080’,
‘http://api.meiduo.site:8000’,
‘http://www.meiduo.site’, # 添加
)
CORS_ALLOW_CREDENTIALS = True # 允许携带cookie
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’,
]
ALLOWED_HOSTS = [’*’]
另一种方法:
# 跨域增加忽略
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'*'
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
)
#部署到云服务上必备
ALLOWED_HOSTS = ['*']