Django:如何管理开发和生产设置? [英] Django: How to manage development and production settings?

查看:189
本文介绍了Django:如何管理开发和生产设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个基本的应用程序。现在在部署阶段,已经很清楚我需要本地设置和生产设置。



很高兴知道以下内容:




  • 如何最好地处理开发和生产设置。

  • 如何在开发环境中保留django-debug-toolbar等应用程序。

  • 开发和部署设置的其他提示和最佳做法


解决方案

我最喜欢的方法是使用 DJANGO_SETTINGS_MODULE 环境变量,并使用两个(或更多)设置文件,例如 production_settings.py test_settings.py



然后,您可以使用引导脚本或进程管理器加载正确的设置(通过设置环境)。如果你使用的是virtualenv,那么你甚至可以将它们嵌入到virtualenv的激活脚本中。



你可以利用 PYTHONPATH 变量将设置存储在完全不同的位置(例如,在生产服务器上,将它们存储在 / etc / 中)是有意义的) - 这样可以更容易地部署您完全将配置与应用程序文件(这是一件好事)完全分离。



使用Process Manager设置 DJANGO_SETTINGS_MODULE / h2>

如果你不喜欢编写一个设置环境的引导脚本(有非常好的理由感觉到这种方式!),我建议使用进程管理器:





黑客 bin / activate 设置 DJANGO_SETTINGS_MODULE



如果使用virtualenv,则将其附加到您的 bin / activate 脚本中:

  DJANGO_SETTINGS_MODULE =production_settings
export DJANGO_SETTINGS_MODULE

服务器:

  DJANGO_SETTINGS_MODULE =test_settings
export DJANGO_SETTINGS_MODULE
pre>

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:

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

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