与多个开发人员一起管理 Django 本地和站点设置?

发布于 2024-12-10 03:58:44 字数 1014 浏览 0 评论 0原文

环顾四周后,我想出了以下代码,这似乎工作得很好,我想知道其他人想出了什么,并且反馈会很棒。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心欲静而疯不止 2024-12-17 03:58:44

我认为虽然您的代码可能有效,但它不必要地复杂。复杂的代码很少是一件好事,因为它很难调试,并且您的设置模块是您最不想在 Django 项目中引入错误的地方。

拥有一个 settings.py 文件会更容易,其中包含生产服务器的所有设置以及所有开发计算机通用的设置,并在其底部导入 local_settings.py 。然后,开发人员将在 local_settings.py 中添加特定于其计算机的设置。

settings.py

# all settings for the production server, 
# and settings common to all development machines eg. 
# INSTALLED_APPS, TEMPLATE_DIRS, MIDDLEWARE_CLASSES etc.

# Import local_settings at the very bottom of the file
# Use try|except block since we won't have this on the production server, 
# only on dev machines
try:
    from local_settings import *
except ImportError:
    pass

local_settings.py

# settings specific to the dev machine
# eg DATABASES, MEDIA_ROOT, etc
# You can completely override settings in settings.py 
# or even modify them eg:

from settings import INSTALLED_APPS, MIDDLEWARE_CLASSES # Due to how python imports work, this won't cause a circular import error

INSTALLED_APPS += ("debug_toolbar",)
MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)

只需记住不要在生产服务器上上传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 a local_settings.py imported at the bottom of it. The local_settings.py would then be where the developers add settings specific to their machines.

settings.py:

# all settings for the production server, 
# and settings common to all development machines eg. 
# INSTALLED_APPS, TEMPLATE_DIRS, MIDDLEWARE_CLASSES etc.

# Import local_settings at the very bottom of the file
# Use try|except block since we won't have this on the production server, 
# only on dev machines
try:
    from local_settings import *
except ImportError:
    pass

local_settings.py

# settings specific to the dev machine
# eg DATABASES, MEDIA_ROOT, etc
# You can completely override settings in settings.py 
# or even modify them eg:

from settings import INSTALLED_APPS, MIDDLEWARE_CLASSES # Due to how python imports work, this won't cause a circular import error

INSTALLED_APPS += ("debug_toolbar",)
MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)

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 the local_settings.py file is ignored.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文