如何在django模板中检查TEMPLATE_DEBUG标志? [英] How to check the TEMPLATE_DEBUG flag in a django template?

查看:620
本文介绍了如何在django模板中检查TEMPLATE_DEBUG标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果TEMPLATE_DEBUG标志被设置,你知道是否可以在django模板中知道?

Do you know if it is possible to know in a django template if the TEMPLATE_DEBUG flag is set?

我想在我离开时禁用我的谷歌分析脚本在我的开发机器上运行我的django应用程序。像{%if debug%}模板标签的东西将是完美的。不幸的是,我没有在文档中找到类似的东西。

I would like to disable my google analytics script when I am running my django app on my development machine. Something like a {% if debug %} template tag would be perfect. Unfortunately, I didn't find something like that in the documentation.

肯定地,我可以添加这个标志到上下文,但我想知道是否有

For sure, I can add this flag to the context but I would like to know if there is a better way to do that.

推荐答案

假设您尚未设置 TEMPLATE_CONTEXT_PROCESSORS settings.py 中的某些其他值,Django将自动加载 debug 上下文预处理器(如所指出的 here )。这意味着如果 settings.DEBUG 中调用 debug 的变量c>是true 您的本地计算机的IP地址(可以简单地为127.0.0.1)设置在变量 settings.INTERNAL_IPS (这是描述了此处)。 settings.INTERNAL_IPS 是Django应该识别为内部的元组或IP地址列表。

Assuming you haven't set TEMPLATE_CONTEXT_PROCESSORS to some other value in settings.py, Django will automatically load the debug context preprocessor (as noted here). This means that you will have access to a variable called debug in your templates if settings.DEBUG is true and your local machine's IP address (which can simply be 127.0.0.1) is set in the variable settings.INTERNAL_IPS (which is described here). settings.INTERNAL_IPS is a tuple or list of IP addresses that Django should recognize as "internal".

这篇关于如何在django模板中检查TEMPLATE_DEBUG标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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