Django:django-cors-headers 報錯no module named "corsheaders"

django跨域使用python

pip install django-cors-headersdjango

而後在settings文件中加上參數設置跨域



# app配置
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    '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',
]
# 而後在setting文件的空白處加上以下這些參數配置:
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',
'Pragma',
)

 

運行Django程序的時候若是報錯,請檢查pip的版本問題。session

我是用的Django==2.1.7,python==3.6,而後pip3 install django-cors-headersapp

運行程序報錯,顯示:cors

"No module named corsheaders"

很顯然我確實下載了模塊,可是程序沒法識別它,找了不少博客,後來在stark overflow上看到了解決方案,我用pip install django-cors-headers,而後命令行報錯,讓我把「--user」的potion加上,因而我用的pip install --user django-cors-headersspa

以後就能夠用了,按照如上的參數在setting文件中配置好便可。命令行

一開始很費解,在網上找到大量的博客都是一句話解決問題pip install便可,沒有過多贅述,我明明都是已經pip過了,配置也是copy的,可是不起做用,後來太晚了就睡覺了,而後次日起來換了一個角度,把上面這條簡單的報錯信息貼上research一下,看看有沒有人遇到過相似的問題,而後才找到緣由,是pip版本的問題,很容易就解決了。code

遇到問題不可鑽牛角尖,必定要多轉換角度去思考,會有解決方案的。csrf

相關文章
相關標籤/搜索