我有一个完全在本地运行的django应用程序,但是当我尝试在heroku上部署它时,我继续遇到此错误,
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
这是我的设置文件
import os
import dj_database_url
# 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/2.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 = True
ALLOWED_HOSTS = ['0.0.0.0', 'localhost', '127.0.0.1', '.herokuapp.com', ]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'phonenumber_field',
'aggregator',
'bouncer',
]
AUTH_USER_MODEL = 'bouncer.User'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'verification@sampleapp.com'
EMAIL_HOST_PASSWORD = '####'
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.ftb_core_api.mail.backends.smtp.EmailBackend'
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',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'sampleapp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'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 = 'sampleapp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DB_NAME', ''),
'USER': os.environ.get('DB_USER', ''),
'PASSWORD': os.environ.get('DB_PASS', ''),
'HOST': 'localhost',
'PORT': '5432',
}
}
# for dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
# Password validation
# https://docs.djangoproject.com/en/2.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/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Los_Angeles'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# REST Framework
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
)
}
这是我的wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ftb_core_backend.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
很明显我的DJANGO_SETTINGS_MODULE已定义。
我找不到调用LOGGING_COFIG的行抛出错误。我也不知道调用setup()的位置。
这里是完整的堆栈跟踪,
Starting process with command `gunicorn ftb_core_backend.wsgi —-log-file -`
2018-02-28T17:56:52.287605+00:00 app[web.1]: [2018-02-28 17:56:52 +0000] [4] [INFO] Starting gunicorn 19.7.1
2018-02-28T17:56:52.288193+00:00 app[web.1]: [2018-02-28 17:56:52 +0000] [4] [INFO] Listening at: http://0.0.0.0:58538 (4)
2018-02-28T17:56:52.288393+00:00 app[web.1]: [2018-02-28 17:56:52 +0000] [4] [INFO] Using worker: sync
2018-02-28T17:56:52.295020+00:00 app[web.1]: [2018-02-28 17:56:52 +0000] [8] [INFO] Booting worker with pid: 8
2018-02-28T17:56:52.390046+00:00 app[web.1]: [2018-02-28 17:56:52 +0000] [9] [INFO] Booting worker with pid: 9
2018-02-28T17:56:52.832612+00:00 heroku[web.1]: State changed from starting to up
2018-02-28T17:56:53.050445+00:00 heroku[web.1]: Process exited with status 3
2018-02-28T17:56:52.796081+00:00 app[web.1]: [2018-02-28 17:56:52 +0000] [8] [ERROR] Exception in worker process
2018-02-28T17:56:52.796116+00:00 app[web.1]: Traceback (most recent call last):
2018-02-28T17:56:52.796118+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
2018-02-28T17:56:52.796120+00:00 app[web.1]: worker.init_process()
2018-02-28T17:56:52.796121+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process
2018-02-28T17:56:52.796123+00:00 app[web.1]: self.load_wsgi()
2018-02-28T17:56:52.796125+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
2018-02-28T17:56:52.796126+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2018-02-28T17:56:52.796128+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2018-02-28T17:56:52.796129+00:00 app[web.1]: self.callable = self.load()
2018-02-28T17:56:52.796131+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
2018-02-28T17:56:52.796132+00:00 app[web.1]: return self.load_wsgiapp()
2018-02-28T17:56:52.796134+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
2018-02-28T17:56:52.796136+00:00 app[web.1]: return util.import_app(self.app_uri)
2018-02-28T17:56:52.796137+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 352, in import_app
2018-02-28T17:56:52.796139+00:00 app[web.1]: __import__(module)
2018-02-28T17:56:52.796142+00:00 app[web.1]: application = get_wsgi_application()
2018-02-28T17:56:52.796140+00:00 app[web.1]: File "/app/ftb_core_backend/wsgi.py", line 15, in <module>
2018-02-28T17:56:52.796144+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2018-02-28T17:56:52.796145+00:00 app[web.1]: django.setup(set_prefix=False)
2018-02-28T17:56:52.796147+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
2018-02-28T17:56:52.796148+00:00 app[web.1]: configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2018-02-28T17:56:52.796150+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
2018-02-28T17:56:52.796152+00:00 app[web.1]: self._setup(name)
2018-02-28T17:56:52.796154+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
2018-02-28T17:56:52.796155+00:00 app[web.1]: % (desc, ENVIRONMENT_VARIABLE))
2018-02-28T17:56:52.796203+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
我在这里做错了什么?
答案 0 :(得分:2)
当您没有定义DJANGO_SETTINGS_MODULE
环境变量时,通常会发生这种情况。
在这种情况下,您的.wsgi
文件包含:
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ftb_core_backend.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
并且错误来自第一行调用get_wsgi_application()
,该调用没有在环境中定义DJANGO_SETTINGS_MODULE
。
答案 1 :(得分:0)
设置环境时遇到了同样的问题。
我的解决方案是打开终端并手动设置环境
以下是步骤:(对于Mac)
打开终端并输入vim〜/ .bash_profile
添加以下行:
PATH=${PATH}:/Users/work/workspace/[virtual_environment]/bin
PYTHONPATH=${PATH}:/Users/work/PycharmProjects/[project_name]
DJANGO_SETTINGS_MODULE=[project_name].settings
键入:wq
保存并退出终端
键入源〜/ .bash_profile以重新加载它