与多个开发人员一起管理 Django 本地和站点设置?
环顾四周后,我想出了以下代码,这似乎工作得很好,我想知道其他人想出了什么,并且反馈会很棒。
settings/init.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
设置/machine_name_local.py //开发人员或主机服务器的计算机特定设置
from settings.site import *
LOCAL = 3
After looking around I have come up with the following code this seems to work well I was wondering what others have come up with and feedback would be great.
settings/init.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 //project specific
from settings.base import *
SITE = 2
LOCAL = 2
settings/machine_name_local.py //machine specific settings for developers or host server
from settings.site import *
LOCAL = 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为虽然您的代码可能有效,但它不必要地复杂。复杂的代码很少是一件好事,因为它很难调试,并且您的设置模块是您最不想在 Django 项目中引入错误的地方。
拥有一个
settings.py
文件会更容易,其中包含生产服务器的所有设置以及所有开发计算机通用的设置,并在其底部导入local_settings.py
。然后,开发人员将在local_settings.py
中添加特定于其计算机的设置。settings.py:
local_settings.py
只需记住不要在生产服务器上上传
local_settings.py
,并且如果您使用的是 VCS,请将其配置为忽略local_settings.py
文件。I think that while your code probably works, it's unnecessarily complicated. Complicated code is rarely a good thing because it's hard to debug, and your settings module in the last place you want to introduce errors in your Django project.
It's easier to have a
settings.py
file, with all settings for the production server plus settings common to all development machines and have alocal_settings.py
imported at the bottom of it. Thelocal_settings.py
would then be where the developers add settings specific to their machines.settings.py:
local_settings.py
Just remember not to upload the
local_settings.py
on the production server, and if you are using a VCS, to configure it such that thelocal_settings.py
file is ignored.