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?
我是Django的新手,我想知道是否有一种方法可以将模板可用的所有变量转储为调试目的。在Python中,我可能会使用一些类似于local()的东西,对于默认模板引擎,是否有一些相同的东西?
Note: suppose I don't have access to the view for the purposes of this question.
注意:假设出于这个问题的目的,我没有访问视图的权限。
4 个解决方案
#1
54
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 %}
{ %调试% }
内置:调试
More information in the context_processor.debug including:
更多关于context_processor.debug的信息,包括:
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如果启用了这个处理器,那么每个RequestContext都将包含调试和sql_queries变量——但前提是您的调试设置为True,并且请求的IP地址(request. meta ['REMOTE_ADDR'])位于INTERNAL_IPS设置中
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.
类似于Peter G的建议,我经常在有显示的页面末尾使用
{% debug|escape %}
#3
9
You might also be interested in django-template-repl, a readline shell for the Django template language. You can drop a {% load repl %}{% pdb %}
into your template and get an interactive debugger.
您可能还对Django -template-repl感兴趣,它是Django模板语言的readline shell。您可以将{% load repl %}{% pdb %}放入模板并获得交互式调试器。
#4
5
While the two solutions provided by the other members may get you access to all the variables in a template I thought there had to be an easier way (thanks for your responses, BTW).
虽然其他成员提供的两个解决方案可以让您访问模板中的所有变量,但我认为必须有一个更简单的方法(顺便说一下,感谢您的响应)。
Here is a simple way to find all the variables passed to the template.
下面是一种找到传递给模板的所有变量的简单方法。
- Introduce an error into the template you want to examine. Adding a non-existent tag works.
- 在要检查的模板中引入一个错误。添加不存在的标记是可行的。
- Ensure that debugging is ON.
- 确保调试已经启动。
- Browse to the page that loads the template. (your site must be running via runserver or some other means).
- 浏览到加载模板的页面。(您的站点必须通过runserver或其他方式运行)。
The debugging output for the template contains a section called "TraceBack". Find the traceback for your view (second entry from the top in my case) and click on "Local vars". And it's all there.
模板的调试输出包含一个名为“TraceBack”的部分。找到你的视图的回溯(在我的例子中从顶部的第二个条目),然后点击“本地vars”。都是在那里。
#1
54
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 %}
{ %调试% }
内置:调试
More information in the context_processor.debug including:
更多关于context_processor.debug的信息,包括:
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如果启用了这个处理器,那么每个RequestContext都将包含调试和sql_queries变量——但前提是您的调试设置为True,并且请求的IP地址(request. meta ['REMOTE_ADDR'])位于INTERNAL_IPS设置中
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.
类似于Peter G的建议,我经常在有显示的页面末尾使用
{% debug|escape %}
#2
#3
9
You might also be interested in django-template-repl, a readline shell for the Django template language. You can drop a {% load repl %}{% pdb %}
into your template and get an interactive debugger.
您可能还对Django -template-repl感兴趣,它是Django模板语言的readline shell。您可以将{% load repl %}{% pdb %}放入模板并获得交互式调试器。
#4
5
While the two solutions provided by the other members may get you access to all the variables in a template I thought there had to be an easier way (thanks for your responses, BTW).
虽然其他成员提供的两个解决方案可以让您访问模板中的所有变量,但我认为必须有一个更简单的方法(顺便说一下,感谢您的响应)。
Here is a simple way to find all the variables passed to the template.
下面是一种找到传递给模板的所有变量的简单方法。
- Introduce an error into the template you want to examine. Adding a non-existent tag works.
- 在要检查的模板中引入一个错误。添加不存在的标记是可行的。
- Ensure that debugging is ON.
- 确保调试已经启动。
- Browse to the page that loads the template. (your site must be running via runserver or some other means).
- 浏览到加载模板的页面。(您的站点必须通过runserver或其他方式运行)。
The debugging output for the template contains a section called "TraceBack". Find the traceback for your view (second entry from the top in my case) and click on "Local vars". And it's all there.
模板的调试输出包含一个名为“TraceBack”的部分。找到你的视图的回溯(在我的例子中从顶部的第二个条目),然后点击“本地vars”。都是在那里。