如何在django模板中运行此代码 [英] how to run this code in django template
问题描述
这是我的代码:
{% for i,j in enumerate(a) %}
{{i}} ,{{j}}
{% endfor%}
它显示错误,我认为它不能运行枚举
方法,
but , it show a error , i think it cant run the enumerate
method ,
所以如何运行 enumerate
在django模板中,
so how to run the enumerate
in django template ,
谢谢
推荐答案
模板子系统具有内置于for / endfor块中的一些特殊结构,允许您访问循环的当前索引,而无需调用 enumerate
。
The template subsystem has some special constructs built into the for/endfor block that allows you to access the current index of the loop without having to call enumerate
.
{% for j in a %}
{{ forloop.counter0 }}, {{ j }}
{% endfor %}
虽然此代码段可以解决您的直接问题,但如果您期待要访问Django模板中的Python内建和其他Python构造,您可能会误解它提供/强制的沙盒。
While this snippet solves your immediate problem, if you're expecting to have access to Python builtins and other Python constructs inside your Django templates, you may be misunderstanding the sandbox that it provides/enforces.
这篇关于如何在django模板中运行此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!