【发布时间】:2011-12-08 03:10:07
【问题描述】:
环顾四周后,我想出了以下代码,这似乎运行良好,我想知道其他人想出了什么,反馈会很棒。
设置/初始化.py
import sys
import socket
# try to import settings file based on machine name.
try:
settings = sys.modules[__name__]
host_settings_module_name = '%s' % (socket.gethostname().replace('.', '_'))
host_settings = __import__(host_settings_module_name, globals(), locals(), ['*'], -1)
# Merge imported settings over django settings
for key in host_settings.__dict__.keys():
if key.startswith('_'): continue #skip privates and __internals__
settings.__dict__[key] = host_settings.__dict__[key]
except Exception, e:
print e
from settings.site import *
settings/base.py
BASE = 1
SITE = 1
LOCAL = 1
settings/site.py //项目特定
from settings.base import *
SITE = 2
LOCAL = 2
settings/machine_name_local.py //针对开发人员或主机服务器的机器特定设置
from settings.site import *
LOCAL = 3
【问题讨论】:
标签: django settings dev-to-production