为本地开发和生产服务器处理设置的推荐方法是什么?其中一些(如常量等)可以在两者中更改/访问,但其中一些(如静态文件的路径)需要保持不同,因此不应该在每次部署新代码时都重写。
目前,我正在将所有常量添加到settings.py中。但是每次我在本地更改一些常量时,我都必须将其复制到生产服务器并编辑文件以进行特定于生产的更改……:(
编辑:看起来这个问题没有标准答案,我已经接受了最流行的方法。
为本地开发和生产服务器处理设置的推荐方法是什么?其中一些(如常量等)可以在两者中更改/访问,但其中一些(如静态文件的路径)需要保持不同,因此不应该在每次部署新代码时都重写。
目前,我正在将所有常量添加到settings.py中。但是每次我在本地更改一些常量时,我都必须将其复制到生产服务器并编辑文件以进行特定于生产的更改……:(
编辑:看起来这个问题没有标准答案,我已经接受了最流行的方法。
当前回答
创建多个版本的settings.py是12因素应用程序方法论的反模式。 请使用python- decoupling或django-environ。
其他回答
我使用了上面提到的jpartogi的一个变体,我觉得它更简短:
import platform
from django.core.management import execute_manager
computername = platform.node()
try:
settings = __import__(computername + '_settings')
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file '%r_settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file local_settings.py does indeed exist, it's causing an ImportError somehow.)\n" % (computername, __file__))
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)
基本上,在每台计算机(开发或生产)上,我都有适当的动态加载的hostname_settings.py文件。
如果你愿意,可以选择维护不同的文件: 如果您正在使用git或任何其他VCS将代码从本地推送到服务器,您可以将设置文件添加到.gitignore。
这将允许您在两个地方有不同的内容而没有任何问题。所以在服务器上,你可以配置一个独立版本的settings.py,任何在本地所做的更改都不会反映在服务器上,反之亦然。
此外,它将删除settings.py文件从github也,大错误,这我已经看到许多新手做。
大多数解决方案的问题是,您要么将本地设置应用在常用设置之前,要么应用在常用设置之后。
所以不可能覆盖
特定于env的设置定义了memcached池的地址,在主设置文件中,这个值用于配置缓存后端 特定环境的设置添加或删除应用程序/中间件到默认的
同时。
一种解决方案可以使用“ini”风格的配置文件和ConfigParser类实现。它支持多个文件、延迟字符串插值、默认值和许多其他优点。 一旦加载了许多文件,就可以加载更多的文件,它们的值将覆盖之前的文件(如果有的话)。
您可以加载一个或多个配置文件,这取决于计算机地址、环境变量,甚至是先前加载的配置文件中的值。然后只需使用解析后的值填充设置。
我成功使用的一个策略是:
Load a default defaults.ini file Check the machine name, and load all files which matched the reversed FQDN, from the shortest match to the longest match (so, I loaded net.ini, then net.domain.ini, then net.domain.webserver01.ini, each one possibly overriding values of the previous). This account also for developers' machines, so each one could set up its preferred database driver, etc. for local development Check if there is a "cluster name" declared, and in that case load cluster.cluster_name.ini, which can define things like database and cache IPs
举个例子,你可以定义一个“子域”值per-env,然后在默认设置中使用(hostname: %(subdomain).whatever.net)来定义django工作所需的所有必要的主机名和cookie。
这是我能得到的DRY,大多数(现有的)文件只有3或4个设置。除此之外,我还必须管理客户配置,所以存在一组额外的配置文件(包括数据库名称、用户和密码、分配的子域等),每个客户一个或多个。
您可以根据需要将其扩展到低或高,只需在配置文件中放入您想要在每个环境中配置的键,一旦需要新的配置,将先前的值放在默认配置中,并在必要时覆盖它。
该系统已被证明是可靠的,并与版本控制工作良好。它已经被使用很长时间来管理两个独立的应用程序集群(每台机器上有15个或更多的django站点实例),有超过50个客户,集群的大小和成员会根据系统管理员的情绪而变化……
Django 1.5的最佳实践建议对你的设置文件使用版本控制,并将文件存储在一个单独的目录中:
project/
app1/
app2/
project/
__init__.py
settings/
__init__.py
base.py
local.py
production.py
manage.py
base.py文件包含常见设置(如MEDIA_ROOT或ADMIN),而local.py和production.py有特定于站点的设置:
在基本文件设置/base.py:
INSTALLED_APPS = (
# common apps...
)
在本地开发设置文件settings/local.py中:
from project.settings.base import *
DEBUG = True
INSTALLED_APPS += (
'debug_toolbar', # and other apps for local development
)
在文件生产设置文件设置/production.py:
from project.settings.base import *
DEBUG = False
INSTALLED_APPS += (
# other apps for production site
)
然后当你运行django时,你添加——settings选项:
# Running django for local development
$ ./manage.py runserver 0:8000 --settings=project.settings.local
# Running django shell on the production site
$ ./manage.py shell --settings=project.settings.production
这本书的作者还在Github上发布了一个示例项目布局模板。
为了在不同的环境中使用不同的设置配置,创建不同的设置文件。在部署脚本中,使用——settings=<my-settings.py>参数启动服务器,通过该参数可以在不同的环境中使用不同的设置。
使用这种方法的好处:
Your settings will be modular based on each environment You may import the master_settings.py containing the base configuration in the environmnet_configuration.py and override the values that you want to change in that environment. If you have huge team, each developer may have their own local_settings.py which they can add to the code repository without any risk of modifying the server configuration. You can add these local settings to .gitnore if you use git or .hginore if you Mercurial for Version Control (or any other). That way local settings won't even be the part of actual code base keeping it clean.