如何获取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%}
如果启用此处理器,则每个RequestContext将包含 debug
和 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>
在页面末尾有没有
,但我可以检查调试。
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屋!