gpt4 book ai didi

python - Heroku dynos 在部署后立即崩溃 : DATABASE undefined

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:55 26 4
gpt4 key购买 nike

在 Heroku 和 dynos 上使用 Django 几乎立即崩溃。在日志错误中,它提到未定义“DATABASES”。这是日志:

2014-02-13T11:32:38.373434+00:00 app[web.1]: 2014-02-13 11:32:38 [7] [INFO] Worker exiting (pid: 7)
2014-02-13T11:32:38.373434+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
2014-02-13T11:32:38.373434+00:00 app[web.1]: __import__(name)
2014-02-13T11:32:38.373434+00:00 app[web.1]: NameError: name 'DATABASES' is not defined
2014-02-13T11:32:38.373434+00:00 app[web.1]: DATABASES['default'] = dj_database_url.config()
2014-02-13T11:32:38.569277+00:00 heroku[web.1]: State changed from starting to crashed
2014-02-13T11:32:38.569974+00:00 heroku[web.1]: State changed from crashed to starting
2014-02-13T11:32:38.505867+00:00 app[web.1]: 2014-02-13 11:32:38 [2] [INFO] Shutting down: Master
2014-02-13T11:32:38.505867+00:00 app[web.1]: 2014-02-13 11:32:38 [2] [INFO] Reason: Worker failed to boot.
2014-02-13T11:32:38.557996+00:00 heroku[web.1]: Process exited with status 3
2014-02-13T11:32:39.808509+00:00 heroku[web.1]: Process exited with status 3
2014-02-13T11:32:42.728831+00:00 heroku[web.1]: Starting process with command `gunicorn counter.wsgi`
2014-02-13T11:32:43.803689+00:00 app[web.1]: 2014-02-13 11:32:43 [2] [INFO] Starting gunicorn 18.0
2014-02-13T11:32:43.807267+00:00 app[web.1]: 2014-02-13 11:32:43 [2] [INFO] Listening at: http://0.0.0.0:44456 (2)
2014-02-13T11:32:43.807741+00:00 app[web.1]: 2014-02-13 11:32:43 [2] [INFO] Using worker: sync
2014-02-13T11:32:43.819443+00:00 app[web.1]: 2014-02-13 11:32:43 [7] [INFO] Booting worker with pid: 7
2014-02-13T11:32:43.939156+00:00 app[web.1]: 2014-02-13 11:32:43 [7] [ERROR] Exception in worker process:
2014-02-13T11:32:43.939156+00:00 app[web.1]: Traceback (most recent call last):
2014-02-13T11:32:43.939156+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
2014-02-13T11:32:43.939156+00:00 app[web.1]: worker.init_process()
2014-02-13T11:32:43.939156+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
...
...
...

然后继续。

我相信这可能与我按照 Heroku 设置指南更改 settings.py 时有关。下面附上文件:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

SECRET_KEY = 'c_ctc!-&!zyug%xh3&xi_v#1cs0xkr8!t_+y+f0ecc1)7dypz('
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['*']
<span style="font-family:"Helvetica Neue","Helvetica","Arial",sans-serif;font-size:13px"></span>
<span style="font-family:"Helvetica Neue","Helvetica","Arial",sans-serif;font-size:13px">INSTALLED_APPS = (</span>
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
)

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'counter.urls'
WSGI_APPLICATION = 'counter.wsgi.application'

import dj_database_url
DATABASES['default'] = dj_database_url.config()

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_ROOT = 'staticfiles'

STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

我想知道这是否与 dj_database_url.config() 没有被正确调用有关,因为 dj_database_url 没有被正确导入? settings.py 是根据此处的说明编辑的:https://devcenter.heroku.com/articles/getting-started-with-django#django-settings

另外,当我运行 $>heroku run python manage.py runservers 时,我得到以下信息:

Running `python manage.py runservers` attached to terminal... up, run.2614
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
commands = get_commands()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 128, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/app/counter/settings.py", line 59, in <module>
DATABASES['default'] = dj_database_url.config()
NameError: name 'DATABASES' is not defined

最佳答案

下面是如何将 dj_database_url.config 用于 postgresql 数据库。这同样适用于其他引擎。

DATABASES = {}
DATABASES['default'] = dj_database_url.config(default='postgres://
user:pass@host/db')
DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'

我猜你在使用它之前错过了创建 DATABASES 字典。

关于python - Heroku dynos 在部署后立即崩溃 : DATABASE undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com