在Django项目中根据环境处理不同配置/设置的最佳方式 [英] Best way to handle different configuration/settings based on environment in Django project

查看:594
本文介绍了在Django项目中根据环境处理不同配置/设置的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


DEBUG == False是否意味着应用程序在生产环境中运行?


至少,这就是我偶尔看到在互联网上。但是,请稍后再加入 settings.py ?好的,我可以把本地设置,比如说 settings_local.py ,然后从 settings.py 导入。但是如果某些设置取决于环境,那么比在 import 语句之后要更改。有更多的我想,我越不喜欢它。而你呢?

解决方案

作为这个问题的答案:


DEBUG == False是否意味着应用程序在生产环境中运行?


DEBUG 是您在 setting.py 文件中定义的配置。




  • 如果设置为 True ,如果未处理的异常,它将显示完整的堆栈跟踪以及所有值声明的变量。


  • 如果设置为 False ,则您的服务器只返回 500 没有任何堆栈跟踪的状态代码。




在生产中,您必须 DEBUG 设置为 False 以防止安全漏洞的潜在风险,以及您不想要的其他信息用户要知道。






为了使用不同的设置配置在不同的环境,创建不同的设置文件。在部署脚本中,使用 - settings =< my-settings.py> 参数,您可以在不同的环境中使用不同的设置 p>

使用此方法的好处


  1. 您的设置将基于每个环境进行模块化


  2. 您可以导入包含$的 master_settings.py 基本配置在 environmnet_configuration.py 中,并覆盖您希望在该环境中更改的值。


  3. 如果你有巨大的团队,每个开发人员可能都有自己的 local_settings.py ,他们可以添加到代码库中,而不会有任何修改服务器配置的风险。如果您使用 git .hginore ,则可以将这些本地设置添加到 .gitnore 您可以使用Mercurial 来进行代码版本控制。这样,本地设置甚至不会成为实际代码基础保持清洁的一部分。



Is DEBUG == False supposed to mean that the app is running in production environment?

At least, that's what I see occasionally on the internet. But what do I put in settings.py then? Okay, I can put local settings to, say, settings_local.py and import it from settings.py. But if some settings depend on environment, than I've got to put them after import statement. There more I think about it, the more I don't like it. And you?

解决方案

As an answer to the question:

Is DEBUG == False supposed to mean that the app is running in production environment?

DEBUG is a configuration that you define in your setting.py file.

  • If set to True, in case of un-handled exception it displays the complete stack-trace along with the values of all the declared variables.

  • If set to False, your server just returns the 500 status code without any stack-trace.

In production, you must have DEBUG set to False in order to prevent potential risk of security breach, and other information which you wouldn't want your user to know.


In order to use different settings configuration on different environment, create different settings file. And in your deployment script, start the server using --settings=<my-settings.py> parameter, via which you can use different settings on different environment.

Benefits of using this approach:

  1. Your settings will be modular based on each environment

  2. 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.

  3. 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 Code Version Control. That way local settings won't even be the part of actual code base keeping it clean.

这篇关于在Django项目中根据环境处理不同配置/设置的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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