【发布时间】:2012-06-30 18:42:38
【问题描述】:
这些天我正在学习如何在 BAE 上编程。 BAE 是一个类似 GAE 的云平台。它支持 python 2.7 和 MySQL。但是有一个问题,BAE 在请求中提供了DATABASE 信息。我可以像这样获得这些设置:
port = request.META['HTTP_BAE_ENV_ADDR_SQL_PORT']
但是如何在运行时配置DATABASES 设置?
我尝试为这个问题写一个中间件,像这样:
from django.conf import settings
class bae_database(object):
def process_request(self, request):
BAE_DB = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
},
}
settings.DATABASES = BAE_DB
然后在settings.py 文件中,我将此中间件添加到MIDDLEWARE_CLASSES 并设置DATABASES = {}。
结果,我收到此错误消息:
You haven't set the database ENGINE setting yet.
实际上中间件可以工作,因为在 Django 错误页面的设置部分我得到:
DATABASES
{'default': {'ENGINE': 'django.db.backends.mysql', 'HOST': 'localhost', 'NAME': 'django', 'PASSWORD': '********************', 'PORT': '3306', 'USER': 'root'}}
不知道为什么,我改了DATABASES属性,但是没用。请帮帮我。
【问题讨论】: