【发布时间】:2020-11-01 02:44:08
【问题描述】:
我试图在 PythonAnywhere.com 上部署我的 django-rest-framework api,但我不断收到 django.contrib.session 找不到。
**我已阅读 PythonAnywher.com 上的文档,并执行了以下步骤:(https://help.pythonanywhere.com/pages/DeployExistingDjangoProject) (https://help.pythonanywhere.com/pages/DebuggingImportError)
- 能够自行运行 wsgi.py 文件
- 能够正常运行 import myproject.settings
- 能够运行 python /path/to/myapp/settings.py 且没有错误
- 无法弄清楚他们所说的影子是什么意思。所有的包都是通过pipfile导入virutalenv中的
- 能够验证检查 virtualenv Python 版本是否正确**
Settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'its a secret'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'corsheaders',
'rest_framework',
'menus',
'login',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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',
]
ROOT_URLCONF = 'chefsBackEnd.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
],
},
},
]
WSGI_APPLICATION = 'chefsBackEnd.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# DATABASES = {}
# DATABASES['default'] = dj_database_url.config(conn_max_age=600)
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'},
{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator'},
{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'},
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
SITE_ID = 1
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
# 'rest_framework.authentication.TokenAuthentication',
# 'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = [
# 'http://localhost:3000',
# 'http://localhost:5000',
'https://chefs-table-mock-site.herokuapp.com',
'https://git.heroku.com/phlog-api.git'
]
CORS_ALLOW_METHODS = [
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
]
JWT_AUTH = {
'JWT_RESPONSE_PAYLOAD_HANDLER': 'chefsBackEnd.utils.resp_handler',
'JWT_ALGORITHM': 'HS256',
'JWT_VERIFY': True,
}
CSRF_COOKIE_NAME = "csrftoken"
> Traceback (most recent call last): File
> "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/apps/config.py",
> line 118, in create
> cls = getattr(mod, cls_name)
> **AttributeError: module 'django.contrib' has no attribute 'sessions'** During handling of the above exception, another exception
> occurred: Traceback (most recent call last): File "manage.py", line
> 21, in <module>
> main() File "manage.py", line 17, in main
> execute_from_command_line(sys.argv) File "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/core/management/__init__.py",
> line 401, in execute_f rom_command_line
> utility.execute() File "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/core/management/__init__.py",
> line 377, in execute
> django.setup() File "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/__init__.py",
> line 24, in setup
> apps.populate(settings.INSTALLED_APPS) File "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/apps/registry.py",
> line 91, in populate
> app_config = AppConfig.create(entry) File "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/apps/config.py",
> line 136, in create
> import_module(entry) File "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/importlib/__init__.py",
> line 127, in import_module
> return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1014, in _gcd_import File
> "<frozen importlib._bootstrap>", line 991, in _find_and_load File
> "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
> **ModuleNotFoundError: No module named 'django.contrib.sessions'**
基于ImportError: No module named 'django.contrib.sessions',他们能够将 django 的版本更改为我必须让它运行的版本,但我一直遇到同样的问题。
这一定是这个特定版本的 Django (1.10.5) 的问题,因为更新到 1.10.8 似乎已经解决了这个问题。 -https://stackoverflow.com/questions/51791493/importerror-no-module-named-django-contrib-sessions
另外,这来自 PythonAnywhere.com 上的错误日志:
> 2020-07-08 19:54:28,687: Not Found: /favicon.ico 2020-07-08
> 21:27:50,215: Not Found: /static/admin/css/fonts.css 2020-07-08
> 21:31:05,154: Not Found: /static/admin/css/base.css 2020-07-08
> 21:31:05,157: Not Found: /static/admin/css/login.css 2020-07-08
> 21:31:05,194: Not Found: /static/admin/css/responsive.css 2020-07-08
> 21:31:15,815: Not Found: /static/admin/css/base.css 2020-07-08
> 21:31:15,819: Not Found: /static/admin/css/login.css 2020-07-08
> 21:31:15,825: Not Found: /static/admin/css/responsive.css 2020-07-08
> 21:31:26,680: Not Found: /static/admin/css/base.css 2020-07-08
> 21:31:26,685: Not Found: /static/admin/css/login.css 2020-07-08
> 21:31:26,689: Not Found: /static/admin/css/responsive.css 2020-07-08
> 21:31:36,012: Not Found: /api 2020-07-08 21:34:15,458: Not Found:
> /static/admin/css/fonts.css 2020-07-08 21:37:24,180: Not Found:
> /static/admin/css/fonts.css 2020-07-10 22:25:10,104: Error running
> WSGI application 2020-07-10 22:25:10,124: ModuleNotFoundError: No
> module named 'django.contrib.sessions' 2020-07-10 22:25:10,124: File
> "/var/www/jgartsu12_pythonanywhere_com_wsgi.py", line 16, in <module>
> 2020-07-10 22:25:10,124: application = get_wsgi_application()
> 2020-07-10 22:25:10,124: 2020-07-10 22:25:10,124: File
> "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/core/wsgi.py",
> line 12, in get_wsgi_application 2020-07-10 22:25:10,125:
> django.setup(set_prefix=False) 2020-07-10 22:25:10,125: 2020-07-10
> 22:25:10,125: File
> "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/__init__.py",
> line 24, in setup 2020-07-10 22:25:10,125:
> apps.populate(settings.INSTALLED_APPS) 2020-07-10 22:25:10,125:
> 2020-07-10 22:25:10,125: File
> "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/apps/registry.py",
> line 91, in populate 2020-07-10 22:25:10,125: app_config =
> AppConfig.create(entry) 2020-07-10 22:25:10,125: 2020-07-10
> 22:25:10,126: File
> "/home/jgartsu12/.virtualenvs/chefsBackEnd-virtualenv/lib/python3.8/site-packages/django/apps/config.py",
> line 136, in create 2020-07-10 22:25:10,126: import_module(entry)
【问题讨论】:
-
如果你在你的 virtualenv 中启动一个 bash 控制台(使用 PythonAnywhere 中“Web”页面上的链接),然后运行
pip show django,它会显示什么? -
@GilesThomas 它显示:名称:Django 3.08。但是,问题与文件系统的路径有关。检查下面的答案!感谢您的帮助!
-
好的,很高兴听到您找到了解决方案!
标签: python rest django-rest-framework pythonanywhere