管理多个 settings.py 文件 [英] Managing multiple settings.py files

查看:29
本文介绍了管理多个 settings.py 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在 Django 中管理本地和生产设置?

我已经成功地使用 mod_wsgiApache 的 Web 服务器 上部署了一个 Django 项目.

I have managed to deploy successfully a Django project on Apache's Web Server with mod_wsgi.

我想要一些关于如何管理多个 settings.py 文件的建议.现在我有一个用于开发,另一个用于生产(关于数据库参数、静态内容本地化等).我的 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() 来确定当前服务器的类型机器是:它返回developmentproductiontesting.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆