为本地开发和生产服务器处理设置的推荐方法是什么?其中一些(如常量等)可以在两者中更改/访问,但其中一些(如静态文件的路径)需要保持不同,因此不应该在每次部署新代码时都重写。

目前,我正在将所有常量添加到settings.py中。但是每次我在本地更改一些常量时,我都必须将其复制到生产服务器并编辑文件以进行特定于生产的更改……:(

编辑:看起来这个问题没有标准答案,我已经接受了最流行的方法。


当前回答

对于我的大多数项目,我使用以下模式:

创建settings_base.py,其中存储所有环境的通用设置 每当我需要使用具有特定要求的新环境时,我都会创建新的设置文件(例如。Settings_local.py),它继承settings_base.py的内容,并覆盖/添加适当的设置变量(从settings_base import *)

(要使用自定义设置文件运行manage.py,只需使用——settings命令option: manage.py <命令>——settings=settings_you_wish_to_use.py)

其他回答

1 -在你的应用程序中创建一个新文件夹,并设置它的名称。

2 -现在在其中创建一个新的__init__.py文件,并在其中写入

from .base import *

try:
    from .local import *
except:
    pass

try:
    from .production import *
except:
    pass

3 -在设置文件夹local.py和production.py和base.py中创建三个新文件。

4 -在base.py中,复制之前settings.py文件夹中的所有内容,并将其重命名为old_settings.py。

5 -在base.py中更改BASE_DIR路径以指向新的设置路径

旧的路径- > BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

新建路径-> BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

通过这种方式,项目总监可以结构化,并且可以在生产和本地开发之间进行管理。

还有Django Classy Settings。我个人是它的忠实粉丝。它是由Django IRC上最活跃的人之一构建的。你可以使用环境变量来设置。

http://django-classy-settings.readthedocs.io/en/latest/

为了在不同的环境中使用不同的设置配置,创建不同的设置文件。在部署脚本中,使用——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.

我将我的设置拆分如下

settings/
     |
     |- base.py
     |- dev.py
     |- prod.py  

我们有三种环境

dev 暂存 生产

现在,显然登台和生产应该有尽可能相似的环境。所以我们同时保留了prod.py。

但是在这种情况下,我必须确定正在运行的服务器是生产服务器。@T。斯通的回答帮助我写了如下支票。

from socket import gethostname, gethostbyname  
PROD_HOSTS = ["webserver1", "webserver2"]

DEBUG = False
ALLOWED_HOSTS = [gethostname(), gethostbyname(gethostname()),]


if any(host in PROD_HOSTS for host in ALLOWED_HOSTS):
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True  

我使用了上面提到的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文件。