0
這幾天我正在學習如何在BAE上編程。 BAE是一個像GAE一樣的雲平臺。它支持python 2.7和MySQL。但是有一個問題,BAE在請求中提供了DATABASE
信息。我能得到這樣的這些設置:如何在運行時配置Django DATABASES設置?
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
屬性,但它沒用。請幫幫我。
只有當我設置DATABASE = {}時,django纔會連接數據庫,因爲runserver。測試值是我的計算機的工作良好配置。如果我將DATABASES設置爲測試值,它沒有問題。但在BAE上,我可以從請求中獲取配置。 – mata
所以設置靜態設置 – mata
是沒有意義的,但是我所說的是'settings.py'中應該設置'DATABASE'爲非空的有效值。您可以在中間件中更改它,但是當您啓動其他Django時將拒絕運行,您需要在'DATABASES'中有* something *。 – scytale