我是django的新手。我正在通过github在heroku服务器上部署我的简单Web应用程序。但是当我部署它时,我的构建将失败,并且在heroku日志中显示此错误:
-----> Python app detected
-----> Installing python-3.8.3
-----> Installing pip
-----> Installing SQLite3
-----> Installing requirements with pip
Collecting appdirs==1.4.4
Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting asgiref==3.2.10
Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting distlib==0.3.1
Downloading distlib-0.3.1-py2.py3-none-any.whl (335 kB)
Collecting Django==3.0.8
Downloading Django-3.0.8-py3-none-any.whl (7.5 MB)
Collecting django-filter==2.3.0
Downloading django_filter-2.3.0-py3-none-any.whl (73 kB)
Collecting filelock==3.0.12
Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting gunicorn==20.0.4
Downloading gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)
Collecting Pillow==7.2.0
Downloading Pillow-7.2.0-cp38-cp38-manylinux1_x86_64.whl (2.2 MB)
Collecting pytz==2020.1
Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
Collecting six==1.15.0
Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting sqlparse==0.3.1
Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
Collecting virtualenv==20.0.26
Downloading virtualenv-20.0.26-py2.py3-none-any.whl (4.9 MB)
Collecting virtualenvwrapper-win==1.2.6
Downloading virtualenvwrapper-win-1.2.6.tar.gz (21 kB)
Collecting whitenoise==5.1.0
Downloading whitenoise-5.1.0-py2.py3-none-any.whl (19 kB)
Building wheels for collected packages: virtualenvwrapper-win
Building wheel for virtualenvwrapper-win (setup.py): started
Building wheel for virtualenvwrapper-win (setup.py): finished with status 'done'
Created wheel for virtualenvwrapper-win: filename=virtualenvwrapper_win-1.2.6-py3-none-any.whl size=18609 sha256=2894b9f0d8294f17c34d3498acb2ec9baba93dd1039a36a8e44c1252d8d2bb41
Stored in directory: /tmp/pip-ephem-wheel-cache-lqj2a4ec/wheels/66/25/f7/f4b3539b1cc86289c4e24e637c09a39f8f2ab6de61928f0be1
Successfully built virtualenvwrapper-win
Installing collected packages: appdirs, asgiref, distlib, pytz, sqlparse, Django, django-filter, filelock, gunicorn, Pillow, six, virtualenv, virtualenvwrapper-win, whitenoise
Successfully installed Django-3.0.8 Pillow-7.2.0 appdirs-1.4.4 asgiref-3.2.10 distlib-0.3.1 django-filter-2.3.0 filelock-3.0.12 gunicorn-20.0.4 pytz-2020.1 six-1.15.0 sqlparse-0.3.1 virtualenv-20.0.26 virtualenvwrapper-win-1.2.6 whitenoise-5.1.0
-----> $ python CommonModules/manage.py collectstatic --noinput
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 25, in <module>
import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "CommonModules/manage.py", line 21, in <module>
main()
File "CommonModules/manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
django.setup()
File "/app/.heroku/python/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/app/.heroku/python/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/app/.heroku/python/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 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/.heroku/python/lib/python3.8/site-packages/django/contrib/auth/models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/app/.heroku/python/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/models/base.py", line 121, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/models/base.py", line 325, in add_to_class
value.contribute_to_class(cls, name)
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/models/options.py", line 208, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/__init__.py", line 28, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/utils.py", line 207, in __getitem__
backend = load_backend(db['ENGINE'])
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 29, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
! Error while running '$ python CommonModules/manage.py collectstatic --noinput'.
See traceback above for details.
You may need to update application code to resolve this error.
Or, you can disable collectstatic for this application:
$ heroku config:set DISABLE_COLLECTSTATIC=1
https://devcenter.heroku.com/articles/django-assets
! Push rejected, failed to compile Python app.
! Push failed
我在Django项目中的settings.py文件是:
"""
Django settings for common project.
Generated by 'django-admin startproject' using Django 3.0.8.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
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__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '***************************************************'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['common-module.herokuapp.com','127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'user'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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 = 'common.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',
],
},
},
]
WSGI_APPLICATION = 'common.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'common',
'USER':'postgres',
'PASSWORD':'****',
'HOST':'localhost'
}
}
# 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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
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.normpath(os.path.join(BASE_DIR, 'staticfiles'))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
我的requirements.txt文件:
appdirs==1.4.4
asgiref==3.2.10
distlib==0.3.1
Django==3.0.8
django-filter==2.3.0
filelock==3.0.12
gunicorn==20.0.4
Pillow==7.2.0
psycopg2==2.8.5
psycopg2-binary==2.8.5
pytz==2020.1
six==1.15.0
sqlparse==0.3.1
virtualenv==20.0.26
virtualenvwrapper-win==1.2.6
whitenoise==5.1.0
谁能告诉我我做错了什么。以及在heroku上成功部署此应用程序的解决方案。