INSTALLED_APPS = [
'login.apps.LoginConfig',
'mainsaaas.apps.MainsaaasConfig',
'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',
]
CORS_ORIGIN_ALLOW_ALL = True
即使我安装了
pip install django-cors-headers。现在它显示了corsheaders的导入错误,这个我已经检查了虚拟环境和corsheaders包的回溯
> Trace back:
>
>
> Unhandled exception in thread started by <function wrapper at 0x7f5a7d5a1aa0> Traceback (most recent call last): File
> "/home/next/virtual_env/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py",
> line 226, in wrapper
> fn(*args, **kwargs) File "/home/next/virtual_env/venv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py",
> line 113, in inner_run
> autoreload.raise_last_exception() File "/home/next/virtual_env/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py",
> line 249, in raise_last_exception
> six.reraise(*_exception) File "/home/next/virtual_env/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py",
> line 226, in wrapper
> fn(*args, **kwargs) File "/home/next/virtual_env/venv/local/lib/python2.7/site-packages/django/__init__.py",
> line 27, in setup
> apps.populate(settings.INSTALLED_APPS) File "/home/next/virtual_env/venv/local/lib/python2.7/site-packages/django/apps/registry.py",
> line 85, in populate
> app_config = AppConfig.create(entry) File "/home/next/virtual_env/venv/local/lib/python2.7/site-packages/django/apps/config.py",
> line 90, in create
> module = import_module(entry) File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
> __import__(name) ImportError: No module named corsheaders
使用虚拟环境和我的django版本是:Django == 1.10.5
答案 0 :(得分:1)
CorsMiddleware应该尽可能地放置,特别是在任何可以生成响应的中间件之前,例如Django的CommonMiddleware或Whitenoise的WhiteNoiseMiddleware。如果不是之前,它将无法将CORS标头添加到这些响应中。
此外,如果你使用的是CORS_REPLACE_HTTPS_REFERER,它应该放在Django的CsrfViewMiddleware之前
答案 1 :(得分:0)
您可以尝试该命令
pip install django-cors-headers
我使用Django版本:Django == 2.1.7,python版本:Python == 3.6,
首先,我尝试pip3 install django-cors-headers
,
找不到模块名称corsheaders
,
然后我尝试
pip install django-cors-headers
有效
可能对你有帮助
考虑点子的版本
答案 2 :(得分:0)
只需从pip安装
python -m pip install django-cors-headers
然后将其添加到已安装的应用中:
INSTALLED_APPS = [
...
'corsheaders',
...
]
希望它会起作用。如果不起作用,请重新启动服务器并再次运行。