我使用django 1.5.4
遇到了dj_database_url的问题我已经按照文档阅读了相关错误的一些堆栈溢出问题。
一切都在本地开始工作,但在Heroku上我得到一个错误,即没有提供ENGINE。
我还尝试在dj_database_url
内手动填写我的heroku postgres database_url。
相关settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mydb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'myuser',
'PASSWORD': 'admin',
#'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
#'PORT': '', # Set to empty string for default.
}
}
if os.environ.get('DATABASE_URL', None):
import dj_database_url
DATABASES['default'] = dj_database_url.config()
任何建议都会得到很多的感激
修改: 好的,为了帮助调试,我添加了行打印DATABASES,它打印出来:
{'default': {'PASSWORD': None, 'USER': None, 'NAME': 'postgres://<userpassetc>}', 'HOST': None, 'PORT': None}}
所以看起来真的没有提供ENGINE并且dj_database_url正在做一些 funky 但是为什么!?!有任何想法吗?
它清楚地找到我的数据库并将其值插入'NAME',但为什么它没有显示'ENGINE'的值。
修改
好,
我找到了一种黑客的答案。如果有人知道更好的解决方案,请告诉我,我会将其标记为正确。 :)
我只是像以前一样添加了一个if语句,并手动填写了信息,因为似乎问题与dj_database_url没有正确地注入值有关。我不确定。但是接下来我在正常的DATABASE配置下放入我的设置。
settings.py
if os.environ.get('DATABASE_URL', None):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dbnamefromheroku', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'dbuserfromheroku',
'PASSWORD': 'dbpassfromheroku',
'HOST': 'dbhostfromheroku', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': 'dbportfromheroku', # Set to empty string for default.
}
}
如果在8小时内没有更好的答案,我会添加并标记为正确。