Django:如何管理开发和生产设置?

编程入门 行业动态 更新时间:2024-10-10 08:26:35
本文介绍了Django:如何管理开发和生产设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我一直在开发一个基本的应用程序。现在在部署阶段,已经很清楚我需要本地设置和生产设置。

很高兴知道以下内容:

  • 如何最好地处理开发和生产设置。
  • 如何在开发环境中保留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 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:

  • 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:如何管理开发和生产设置?

本文发布于:2023-11-01 01:14:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547819.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!