我对为我的开发和 django 框架做系统管理工作还很陌生。我想为我的本地开发站和我的生产环境使用不同的用户名/密码。
我正在使用 dotcloud 作为服务器。我可以编写一个安装后脚本(使用 python、bash 等),它会在每次新推送时执行。
但是我不知道该怎么做。我需要自己写这个吗?是否有 python/django 构建自动化工具可以帮助我?
澄清:如何在服务器上将 settings.py 中的 debug=false
更改为 true?
最佳答案
django 标准方法是使用环境变量 DJANGO_SETTINGS_MODULE。将它指向不同的设置,并让两者都为常见的事物导入一个通用的设置模块:
# settings_production.py
from settings_common import *
DEBUG = False
DATABASES = {...}
# settings_development.py
from settings_common import *
DEBUG = True
DATABASES = {...}
# settings_common.py
INSTALLED_APPS = (...) # etc
您还可以使用另一种策略,即使用一个主要设置并根据某些系统条件从另一个主要设置导入名称,例如获取 os.platform.node()
或 socket.gethostname ()
并切换该值(或其中的一部分)。
reversed_hostname_parts = socket.gethostname().split('.').reverse()
host_specific = {
('com', 'dotcloud'): 'production',
('local'): 'dev',
}
for index in range(len(reversed_hostname_parts)):
identifier = tuple(reversed_hostname_parts[:index+1])
if identifier in host_specific:
extra_settings = host_specific[identifier]
break
else: # executed when the loop has not been `break`ed
extra_settings = 'dev' # any default value
if extra_settings == 'dev':
from development_settings import *
elif extra_settings == 'production':
from production_settings import *
编辑:添加链接
关于python - 为 Django 构建自动化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7033846/