2

这些天我正在研究如何在 BAE 上编程。BAE 是一个类似 GAE 的云平台。它支持 python 2.7 和 MySQL。但是有一个问题,BAEDATABASE在请求中提供了信息。我可以像这样获得这些设置:

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属性,但是没用。请帮我。

4

1 回答 1

0

DATABASES == {}在吗settings.py?- 如果是这样,请尝试将其设置settings.py为某些内容 - 例如,您当前在中间件中显示的测试值。

于 2012-06-29T08:30:34.007 回答