转换为ConfigParser后,在django中运行syncdb

时间:2015-09-16 05:37:40

标签: python django configparser syncdb

所以我最近根据找到的指南here转换了我的django数据库设置以使用settings.ini文件

我的问题是,现在syncdb已停止为新部署工作:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/usr/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/usr/lib/python2.7/site-packages/django/db/backends/__init__.py", line 160, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/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.

的Settings.ini:

[database]
DATABASE_ENGINE: django.db.backends.postgresql_psycopg2
DATABASE_NAME: postgres
DATABASE_USER: postgres
DATABASE_PASSWORD:
DATABASE_HOST: db
DATABASE_PORT: 5432

settings.py:

import os
import sys
from ConfigParser import RawConfigParser

config = RawConfigParser()
config.read('settings.ini')

DEBUG = bool(os.environ.get('DEBUG', False))
DEBUG_TOOLBAR = bool(os.environ.get('DEBUG_TOOLBAR', False))
TEMPLATE_DEBUG = bool(os.environ.get('TEMPLATE_DEBUG', False))

#Database Config is pulled from the config file
DATABASE_USER = config.get('database', 'DATABASE_USER')
DATABASE_PASSWORD = config.get('database', 'DATABASE_PASSWORD')
DATABASE_HOST = config.get('database', 'DATABASE_HOST')
DATABASE_PORT = config.get('database', 'DATABASE_PORT')
DATABASE_ENGINE = config.get('database', 'DATABASE_ENGINE')
DATABASE_NAME = config.get('database', 'DATABASE_NAME')

我最初开始沿着这条路走下去,所以我们的服务可以在不同的站点运行,并且可以使用易于配置的管理文件进行设置。它还试图使开发环境变得轻松,因为配置不再必须在代码库中,因为它可以存储在其他地方。

我是否实现了错误的操作或者我遇到了捕获22,这种类型的设置更改仅在数据库已同步时才有效?

这是在Django 1.6.5

修改

我忘了提及python manage.py diffsettings返回预期的值,所以我知道manage.py可以正确访问和插入settings.ini文件。

DATABASE_ENGINE = 'django.db.backends.postgresql_psycopg2'  ###
DATABASE_HOST = 'db'  ###
DATABASE_NAME = 'postgres'  ###
DATABASE_PASSWORD = ''  ###
DATABASE_PORT = '5432'  ###
DATABASE_USER = 'postgres'  ###

1 个答案:

答案 0 :(得分:0)

Django的数据库配置格式如:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

查看docs了解详情。

所以你在settings.py中需要这样的东西:

DATABASES = {'default':{}}
#Database Config is pulled from the config file
DATABASES['default']['USER'] = config.get('database', 'DATABASE_USER')
DATABASES['default']['PASSWORD'] = config.get('database', 'DATABASE_PASSWORD')
DATABASES['default']['HOST'] = config.get('database', 'DATABASE_HOST')
DATABASES['default']['PORT'] = config.get('database', 'DATABASE_PORT')
DATABASES['default']['ENGINE'] = config.get('database', 'DATABASE_ENGINE')
DATABASES['default']['NAME'] = config.get('database', 'DATABASE_NAME')