【问题标题】:ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details配置不当:settings.DATABASES 配置不正确。请提供 ENGINE 值。查看设置文档以获取更多详细信息
【发布时间】:2013-07-18 08:31:27
【问题描述】:

我完全按照 heroku 在 https://devcenter.heroku.com/articles/django 上指定的方式进行了操作

但是当我运行heroku run python manage.py syncdb

上面写着ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

这就是我的 settings.py 文件的样子。我尝试了所有我找不到的解决方案似乎没有任何帮助

django_synapse 项目的 Django 设置。

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'h4fm7a&92r1hxa$j&u*7j)61-o2#17k)zy9fncz+7=usn74v%2'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'django_synapse.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'django_synapse.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

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

【问题讨论】:

    标签: django heroku


    【解决方案1】:

    Sultan 和 Joe 都是正确的,如果您将它们放在一起并稍微挖掘一下就可以了,我只是想通过对我有所帮助的补充来更清楚地说明它。 Heroku 网站上的这些 instructions 解释了如何设置它,包括苏丹提到的内容,将此部分添加到 settings.py:

    # Parse database configuration from $DATABASE_URL
    import dj_database_url
    DATABASES['default'] =  dj_database_url.config()
    
    # Honor the 'X-Forwarded-Proto' header for request.is_secure()
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    
    # Allow all host headers
    ALLOWED_HOSTS = ['*']
    
    # Static asset configuration
    import os
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    STATIC_ROOT = 'staticfiles'
    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )
    

    然后,(以及那些说明未提及的内容)在您的 Heroku 帐户中转到您的数据库部分并选择您要使用的数据库。转到“连接设置”(两个相反方向的小箭头)并选择 Django 选项。这段代码应该像 Joe 提到的那样:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', 
            'NAME': 'your_db_name',
            'HOST': 'your_host',
            'PORT': '5432',                    
            'USER': 'your_db_user_name',
            'PASSWORD': 'your_password',                             
        }
    }
    

    将此粘贴​​到您的 settings.py 文件的底部并同步您的数据库。

    【讨论】:

      【解决方案2】:

      您是否尝试过https://devcenter.heroku.com/articles/django#django-settings 文档中的内容?

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

      上面写着

      Django 设置

      接下来,为 Heroku 环境配置应用程序,包括 Heroku 的 Postgres 数据库。 dj-database-url 模块将解析 DATABASE_URL 环境变量的值并将它们转换为 Django 可以理解的东西。

      确保“dj-database-url”在您的需求文件中,然后添加 跟随到您的 settings.py 文件的底部:

      settings.py

      # Parse database configuration from $DATABASE_URL
      import dj_database_url
      DATABASES['default'] =  dj_database_url.config()
      
      # Honor the 'X-Forwarded-Proto' header for request.is_secure()
      SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
      
      # Allow all host headers
      ALLOWED_HOSTS = ['*']
      
      # Static asset configuration
      import os
      BASE_DIR = os.path.dirname(os.path.abspath(__file__))
      STATIC_ROOT = 'staticfiles'
      STATIC_URL = '/static/'
      
      STATICFILES_DIRS = (
          os.path.join(BASE_DIR, 'static'),
      )
      

      【讨论】:

      • 苏丹,我什至必须为 heroku 定义一个 DATABASE_URL 环境变量吗?该值应该是多少?
      • 哦,我现在看到了!但它的价值应该是多少?我很困惑。它应该是一个特定的值还是我可以选择它?
      • 想通了!添加了一个数据库并查看了 heroku 配置以获取 URL。谢谢! :)
      【解决方案3】:

      您定义了两次DATABASES = {'default': {... }}

      解决方案:

      如果您使用 heroku 来为您的数据库提供服务,请使用它

      导入 dj_database_url

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

      如果您尝试连接到外部数据库服务器,请注释上面的两行并使用它

      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
              'NAME': '',                      # Or path to database file if using sqlite3.
              # The following settings are not used with sqlite3:
              'USER': '',
              'PASSWORD': '',
              'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
              'PORT': '',                      # Set to empty string for default.
          }
      }
      

      【讨论】:

        【解决方案4】:

        您尚未定义任何数据库信息。
        您的 DATABASES 部分应如下所示:

        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2', 
                'NAME': 'your_db_name',                     
                'USER': 'your_db_user_name',
                'PASSWORD': 'your_password',
                'HOST': 'ec2-23-21-133-106.compute-1.amazonaws.com', # Or something like this
                'PORT': '5432',                     
            }
        }
        

        【讨论】:

          【解决方案5】:

          你为什么不试试这个:

          DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.postgresql_psycopg2', 
              'NAME': 'yourdatabasename',
              'HOST': 'localhost',
              'PORT': '',                    
              'USER': 'yourusername',
              'PASSWORD': 'yourpassword',
              }
          }
          

          然后应用这个:

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

          【讨论】:

            猜你喜欢
            • 2020-10-07
            • 2014-11-22
            • 2013-11-28
            • 1970-01-01
            • 1970-01-01
            • 2020-04-24
            • 2017-04-21
            • 2012-04-08
            • 1970-01-01
            相关资源
            最近更新 更多