如何获取 Django 模板中定义的所有变量? [英] How do I get all the variables defined in a Django template?
问题描述
我是 Django 的新手,我想知道是否有办法将所有可用的变量转储到模板以进行调试.在 Python 中,我可能会使用诸如 locals()
之类的东西,是否有与默认模板引擎等效的东西?
I'm new to Django and I wonder if there is a way to dump all the variables available to a template for debugging purposes. In Python I might use something like locals()
, is there something equivalent for the default template engine?
注意:假设我无权访问此问题的视图.
Note: suppose I don't have access to the view for the purposes of this question.
推荐答案
Ned 和 blaine 的回答都很好,但如果你真的想完全达到你的要求,那么有一个模板标签:
Both Ned's and blaine's answers are good, but if you really want to achieve exactly what you ask for there's a template tag for it:
{% debug %}
更多信息在 context_processor.debug 包括:
如果启用此处理器,每个 RequestContext 将包含 debugand 和 sql_queries 变量 - 但前提是您的 DEBUG 设置设置为 True 和请求的 IP 地址 (request.META['REMOTE_ADDR']
)处于INTERNAL_IPS设置
If this processor is enabled, every RequestContext will contain debug and and sql_queries variables – but only if your DEBUG setting is set to True and the request’s IP address (
request.META['REMOTE_ADDR']
) is in the INTERNAL_IPS setting
类似于 Peter G 的建议,我经常使用 <div id="django-debug"><pre>{% debug|escape %}</pre></div>
块位于具有 display:none
但我可以检查以进行调试的页面末尾.
Similar to Peter G suggestion, I often use a <div id="django-debug"><pre>{% debug|escape %}</pre></div>
block at the end of the page that has display:none
but that I can inspect to debug.
这篇关于如何获取 Django 模板中定义的所有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!