我正在尝试在 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。 怎么了?
最佳答案
你不能只添加 local_settings.py,你必须明确地导入它。
在你的 settings.py 的最后,添加这个:
try:
from local_settings import *
except ImportError:
pass
try/except block 在那里,以便 Python 在您尚未实际定义 local_settings 文件时忽略这种情况。
关于python - Django 本地设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4909958/