管理多个settings.py文件 [英] Managing multiple settings.py files
问题描述
可能重复:
如何管理Django中的本地vs生产设置?
我已经设法在 Apache的Web服务器上成功部署了一个 Django项目,使用 mod_wsgi
I have managed to deploy successfully a Django project on Apache's Web Server with mod_wsgi
.
我想要一些关于如何管理多个 settings.py
文件的建议。现在我有一个用于开发,一个完全不同于生产(关于DB参数,静态内容本地化,以及类似的东西)。我的 settings.py
文件被版本化(不知道这是否是一个很好的做法),我将其部署如下:
I would like some recommendations on how to manage multiple settings.py
files. Right now I have one for development and one totally different for production (regarding DB parameters, static content localization, and stuff like that). My settings.py
file is versioned (don't know if this is a good practise) and I deploy it with something like:
$ hg archive myproject.tbz2
$ cd /path/of/apache/web/project/location
$ bzip2 -db /home/myself/myproject/myproject.tbz2 | tar -xvf -
可以正常工作。但是我发现自己在操纵多个 settings.py
文件。
It's working OK. But I find myself manipulating multiple settings.py
files.
我想我的问题是:最好的做法是什么关于多个 settings.py
文件版本部署 DJANGO PROJECTS ?
I guess my question is: what are the best practices when deploying DJANGO PROJECTS regarding multiple settings.py
file versions?
推荐答案
我使用的设置模块不是单个文件:
I use a settings module that is not a single file:
settings/
__init__.py
_base.py
_servers.py
development.py
production.py
testing.py
__ init __。py
文件很简单:
from _servers import get_server_type
exec("from %s import *" % get_server_type())
_base.py
文件包含所有服务器类型的所有常用设置。
The _base.py
file contains all of the common settings across all server types.
_servers.py
文件包含使用 get_server_type()
> socket.gethostname()来确定什么当前机器的服务器类型:它返回开发
,生产
或测试
。
The _servers.py
file contains a function get_server_type()
that uses socket.gethostname()
to determine what type of server the current machine is: it returns development
, production
or testing
.
然后其他文件看起来有点像( production.py
):
Then the other files look a bit like (production.py
):
DEBUG=False
TEMPLATE_DEBUG=False
from _base import *
在这些文件的每个文件中,我将设置仅适用于此服务器类型。
In each of these files, I put in the settings that only apply to this server type.
这篇关于管理多个settings.py文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!