NameError:未使用AWS RDS定义名称“DATABASES”

时间:2014-04-22 20:39:37

标签: mysql django heroku amazon-web-services amazon-rds

尝试使用Heroku应用程序配置AWS RDS。我之前使用过postgres,但这是我第一次使用RDS。我在这里跟踪他们的文档(https://devcenter.heroku.com/articles/amazon_rds),但是当我尝试运行syncdb或从Heroku访问dbshel​​l时,我收到以下错误:

$ heroku run ./manage.py syncdb
Running `./manage.py syncdb` attached to terminal... up, run.8947
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 453, 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 263, in fetch_command
app_name = get_commands()[subcommand]
 File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 109, in get_commands
apps = settings.INSTALLED_APPS
 File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
 File "/app/rfp/settings.py", line 33, in <module>
'amazon-rds-ca-cert.pem')}
NameError: name 'DATABASES' is not defined
好的,所以看起来是设置的db配置问题。这是我的相关settings.py

import os
import sys
import dj_database_url

if DEBUG:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(PROJECT_ROOT, 'db.sqlite3'),             
    }
}

else:
     dbconfig = dj_database_url.config()

     if dbconfig:
         DATABASES['default']['OPTIONS'] = {
             'ssl': {'ca': os.path.join(os.path.dirname(__file__), 'etc', 'amazon-rds-ca-cert.pem')}
    }  

然后在heroku中我为DATABASE_URL和DEBUG设置了配置变量:

DATABASE_URL:          mysql2://db_user:db_pw@db_server/db?sslca=config/amazon-rds-ca-cert.pem
DEBUG:                 False

.pem文件位于应用根目录的“etc”文件夹中。有人看到我在设置时错过或配置错误的一步吗?谢谢

1 个答案:

答案 0 :(得分:0)

您的主要问题是您在DATABASES时未设置DEBUG = False。您将变量dbconfig设置为数据库设置,但您从未将其设为default数据库。你需要这样的东西:

else:
     dbconfig = dj_database_url.config()

     if dbconfig:
         DATABASES = {'default': dj_database_url.config()} # Add this
         DATABASES['default']['OPTIONS'] = {
             'ssl': {'ca': os.path.join(os.path.dirname(__file__), 'etc', 'amazon-rds-ca-cert.pem')}
    } 

设置默认数据库后,您可以编辑其中的OPTIONS

编辑:

另外,对于它的价值,this answer表示你不能使用python来构建SSL路径。你需要硬编码。