Django:如何管理开发和生产设置? [英] Django: How to manage development and production settings?
问题描述
很高兴知道以下内容:
- 如何最好地处理开发和生产设置。
- 如何在开发环境中保留django-debug-toolbar等应用程序。
- 开发和部署设置的其他提示和最佳做法
我最喜欢的方法是使用 DJANGO_SETTINGS_MODULE
环境变量,并使用两个(或更多)设置文件,例如 production_settings.py
和 test_settings.py
。
然后,您可以使用引导脚本或进程管理器加载正确的设置(通过设置环境)。如果你使用的是virtualenv,那么你甚至可以将它们嵌入到virtualenv的激活脚本中。
你可以利用 PYTHONPATH
变量将设置存储在完全不同的位置(例如,在生产服务器上,将它们存储在 / etc /
中)是有意义的) - 这样可以更容易地部署您完全将配置与应用程序文件(这是一件好事)完全分离。
使用Process Manager设置 DJANGO_SETTINGS_MODULE
/ h2>
如果你不喜欢编写一个设置环境的引导脚本(有非常好的理由感觉到这种方式!),我建议使用进程管理器:
- 主管可以让你使用程序的
环境将环境变量传递到管理进程nt
配置密钥。 - Honcho (一个纯Python对应的Ruby的Foreman)可以让您在环境(
.env
)文件。
黑客 bin / activate
设置 DJANGO_SETTINGS_MODULE
如果使用virtualenv,则将其附加到您的 bin / activate
脚本中:
DJANGO_SETTINGS_MODULE =production_settings
export DJANGO_SETTINGS_MODULE
服务器:
DJANGO_SETTINGS_MODULE =test_settings
pre>
export DJANGO_SETTINGS_MODULE
I have been developing a basic app. Now at the deployment stage it has become clear I have need for both a local settings and production settings.
It would be great to know the following:
- How best to deal with development and production settings.
- How to keep apps such as django-debug-toolbar only in a development environment.
- Any other tips and best practices for development and deployment settings.
My favorite way of doing this is to use the DJANGO_SETTINGS_MODULE
environment variable and use two (or more) settings files, e.g. production_settings.py
and test_settings.py
.
You can then use a bootstrap script or a process manager to load the correct settings (by setting the environment). If you're using a virtualenv, you could even hack this into the virtualenv's activate script.
You can take advantage of the PYTHONPATH
variable to store the settings in a completely different location (e.g. on a production server, storing them in /etc/
makes sense) — this allows for easier deployment as you totally separate configuration from application files (which is a good thing).
Setting DJANGO_SETTINGS_MODULE
using a Process Manager
If you're not fond of writing a bootstrap script that sets the environment (and there are very good reasons to feel that way!), I would recommend using a process manager:
- Supervisor lets you pass environment variables to managed processes using a program's
environment
configuration key. - Honcho (a pure-Python equivalent of Ruby's Foreman) lets you define environment variables in an "environment" (
.env
) file.
Hacking bin/activate
to set DJANGO_SETTINGS_MODULE
If using a virtualenv, you append this to your bin/activate
script:
DJANGO_SETTINGS_MODULE="production_settings"
export DJANGO_SETTINGS_MODULE
And on your test server:
DJANGO_SETTINGS_MODULE="test_settings"
export DJANGO_SETTINGS_MODULE
这篇关于Django:如何管理开发和生产设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!