Python ImproperlyConfigured:settings.DATABASES 配置不正确。请提供 ENGINE 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26080303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:02:19  来源:igfitidea点击:

ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value

pythondjangoherokudjango-postgresql

提问by ApathyBear

I am in the midst of setting up my django project on heroku. I have been following the documentation, but when I foreman startI receive an error that I can't quite figure out. I have set up my engine files, but it doesn't seem to want to work.

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

Full traceback:

完整追溯:

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.

Someone suggested using ./manage.py diffsettings and showing the DATABASES part:

有人建议使用 ./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': {}}}

I can't seem to figure out what it means, but it doesn't look right on the surface.

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

Here is part of my settings.pythat I think could be relevant to this problem:

这是我的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'),
)

An answer that explains exactly what is wrong and what is going on would be extremely helpful. Thanks in advance.

一个能准确解释错误和正在发生的事情的答案将非常有帮助。提前致谢。

采纳答案by Martijn Pieters

You are using the dj-database-urlmoduleto set DATABASES['default']. Whatever comes before the line:

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

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

is meaningless as you replace your database configuration in its entirety. The dj_database_url.config()loads your database configuration from the DATABASE_URLenvironment variable, or returns {}if the variable is not set.

完全替换数据库配置是没有意义的。在dj_database_url.config()负载从数据库配置DATABASE_URL环境变量,或者返回{}如果变量未设置。

Judging by your error, you didn't set the DATABASE_URLat all. Judging by the code preceding the dj_database_url.config()line, you should not be using the dj_database_url.config()function at all.

从你的错误来看,你根本没有设置DATABASE_URL。通过前面的代码来看dj_database_url.config()行,你不应该使用的dj_database_url.config()功能在所有

If you did want to use it, at least build a default URL:

如果您确实想使用它,请至少构建一个默认 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)}

回答by Piyush S. Wanare

You can use following setting for localhost

您可以对 localhost 使用以下设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'DatabaseName',
        'USER': 'DatabaseUserName',
        'PASSWORD': 'DatabaseUserpassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}