【发布时间】:2011-06-22 01:13:14
【问题描述】:
我正在尝试在 Django 1.2 中使用 local_setting,但它对我不起作用。目前我只是将 local_settings.py 添加到我的项目中。
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'banco1', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '123', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
local_settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'banco2', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': '123', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
问题在于 local_settings.py 没有覆盖 settings.py。 怎么了?
【问题讨论】:
-
在我读到这个问题的那一刻,有三个非常不同且都很有趣的答案。我对丹尼尔的解决方案感到困惑。在我看来,没有万能的解决方案。 Daniel 的解决方案简单高效。 jano 的解决方案以某种方式更清洁,这种方式仅在已经清洁的环境中才有意义,而且成本很低。 John 的解决方案在某种程度上更重量级,但在最受限制的环境中也很有趣。总而言之,只需选择适合您用例的最简单的。