17

我正在 Heroku 上设置我的 Django 项目。我一直在关注文档,但是当foreman start我收到一个我无法弄清楚的错误时。我已经设置了我的引擎文件,但它似乎不想工作。

完整追溯:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/db/backends/__init__.py", line 160, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

有人建议使用./manage.py diffsettings并显示 DATABASES 部分:

DATABASES = {'default': {'AUTOCOMMIT': True, 'ENGINE': 'django.db.backends.dummy', 'ATOMIC_REQUESTS': False, 'NAME': '', 'TEST_MIRROR': None, 'CONN_MAX_AGE': 0, 'TEST_NAME': None, 'TIME_ZONE': 'UTC', 'TEST_COLLATION': None, 'PORT': '', 'HOST': '', 'USER': '', 'TEST_CHARSET': None, 'PASSWORD': '', 'OPTIONS': {}}}

我似乎无法弄清楚这意味着什么,但表面上看起来并不正确。

这是settings.py我认为可能与此问题相关的部分内容:

import os
import dj_database_url

ON_HEROKU = os.environ.get('ON_HEROKU')
HEROKU_SERVER = os.environ.get('HEROKU_SERVER')

if ON_HEROKU:
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'postgresql',
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            'USER': '',
            'PASSWORD': '',
            'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
            'PORT': '',  
        }
    }


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

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

STATIC_URL = '/static/'

# only refers to the location where your static files should end up after running manage.py collectstatic. you shouldn't really need collectstatic) when developing locally
STATIC_ROOT = 'staticfiles'


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

4

4 回答 4

21

您正在使用该dj-database-url模块进行设置DATABASES['default']。行前的任何内容:

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

当您完全替换数据库配置时,这毫无意义。从环境变量dj_database_url.config()加载您的数据库配置,或者如果未设置变量则返回。DATABASE_URL{}

从你的错误来看,你根本没有设置DATABASE_URL。从该行前面的代码来看,您根本dj_database_url.config()不应该使用该dj_database_url.config()函数。

如果您确实想使用它,至少建立一个默认 URL:

if ON_HEROKU:
    DATABASE_URL = 'postgresql://<postgresql>'
else:
    DATABASE_URL = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')

DATABASES = {'default': dj_database_url.config(default=DATABASE_URL)}
于 2014-09-28T00:12:56.603 回答
5

您可以对 localhost 使用以下设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'DatabaseName',
        'USER': 'DatabaseUserName',
        'PASSWORD': 'DatabaseUserpassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
于 2016-04-27T13:11:40.860 回答
2

django-tenant-users我在运行命令时遇到了同样的问题:

python manage.py setup_dtu_tenants

这是因为我的项目文件夹中有一个名为“settings”的文件夹,与我的settings.py. 删除此文件夹后,问题就消失了。

于 2020-11-13T14:20:45.557 回答
0

使用 github 操作运行我的 github 工作流时遇到了这个问题。所以我找到了以下解决方法:

POSTGRES_DB = os.environ.get("POSTGRES_DB") #database name
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD") # database user password
POSTGRES_USER = os.environ.get("POSTGRES_USER") # database username
POSTGRES_HOST = os.environ.get("POSTGRES_HOST") # database host
POSTGRES_PORT = os.environ.get("POSTGRES_PORT") # database port


POSTGRES_READY = (
    POSTGRES_DB is not None
    and POSTGRES_PASSWORD is not None
    and POSTGRES_USER is not None
    and POSTGRES_HOST is not None
    and POSTGRES_PORT is not None
)

print(POSTGRES_READY)

if POSTGRES_READY:
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql",
            "NAME": POSTGRES_DB,
            "USER": POSTGRES_USER,
            "PASSWORD": POSTGRES_PASSWORD,
            "HOST": POSTGRES_HOST,
            "PORT": POSTGRES_PORT,
        }
    }

然后你的.env文件:

export DEBUG=True
export DJANGO_SECRET_KEY=CI_CD_TEST_KEY
export POSTGRES_USER=taxi
export POSTGRES_PASSWORD=taxi
export POSTGRES_DB=pdm
export POSTGRES_PORT=5432
export POSTGRES_HOST=localhost

然后运行source .env

于 2022-01-19T09:00:29.123 回答