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

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

问题描述

您知道在 Django 模板中是否可以知道 TEMPLATE_DEBUG 标志是否已设置?

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.

推荐答案

假设你没有在 settings.py 中将 TEMPLATE_CONTEXT_PROCESSORS 设置为其他值,Django 会自动加载 debug 上下文预处理器(如前所述 这里).这意味着您将可以访问模板中名为 debug 的变量 if settings.DEBUG is true and您本地机器的 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天全站免登陆