0

我设置如下,但有一个错误。我需要你的帮助。

  1. 在setting.py文件中设置corsheader
  2. pip install django-cors-headers 已安装。
  3. pip3 install django-cors-head 已安装。

即使我们如上所述处理它,也有如下错误。

2020-11-10 07:10:05,116: ***************************************************
2020-11-10 07:10:05,117: If you're seeing an import error and don't know why,
2020-11-10 07:10:05,117: we have a dedicated help page to help you debug: 
2020-11-10 07:10:05,117: https://help.pythonanywhere.com/pages/DebuggingImportError/
2020-11-10 07:10:05,117: ***************************************************
2020-11-10 16:11:05,533: Error running WSGI application
2020-11-10 16:11:05,542: ModuleNotFoundError: No module named 'corsheaders'
2020-11-10 16:11:05,543:   File "/var/www/abc_com_wsgi.py", line 16, in <module>
2020-11-10 16:11:05,543:     application = get_wsgi_application()
2020-11-10 16:11:05,543: 
2020-11-10 16:11:05,543:   File "/home/abc/adm/adm/myvenv/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2020-11-10 16:11:05,543:     django.setup(set_prefix=False)
2020-11-10 16:11:05,543: 
2020-11-10 16:11:05,543:   File "/home/abc/adm/adm/myvenv/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
2020-11-10 16:11:05,543:     apps.populate(settings.INSTALLED_APPS)
2020-11-10 16:11:05,544: 
2020-11-10 16:11:05,544:   File "/home/abc/adm/adm/myvenv/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
2020-11-10 16:11:05,544:     app_config = AppConfig.create(entry)
2020-11-10 16:11:05,544: 
2020-11-10 16:11:05,544:   File "/home/abc/adm/adm/myvenv/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
2020-11-10 16:11:05,544:     module = import_module(entry)

设置.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

ALLOWED_HOSTS = [u'abc.com', 'aa.abc.com',]
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',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
]

pip3 cors-headers 安装命令

pip3.7 install --user django-cors-headers      
Looking in links: /usr/share/pip-wheels
Requirement already satisfied: django-cors-headers in ./.local/lib/python3.7/site-packages (3.3.0)
Requirement already satisfied: Django>=2.0 in /usr/lib/python3.7/site-packages (from django-cors-headers) (2.1)
Requirement already satisfied: pytz in /usr/lib/python3.7/site-packages (from Django>=2.0->django-cors-headers) (2018.5)
4

1 回答 1

1

您的 Web 应用程序在虚拟环境中运行,myvenv但您安装了.local带有--user标志的包。您需要在未激活虚拟环境的情况--user下安装它。myvenv请参阅:https ://help.pythonanywhere.com/pages/InstallingNewModules/

于 2020-11-10T11:56:54.397 回答