Joh*_*ohn 3 django django-templates
我试图使用forloop.counter0django模板访问数组的内容,但我不能让它工作.
我拥有的是什么
{% for action in my_action_list %}
{{another_list.forloop.counter0}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
my_action_list列表在哪里,another_list也是列表.我已尝试手动执行此操作,例如{{another_list.0}},这样可行,我也尝试了{{forloop.counter0}},这是打印出正确的索引,因此不确定为什么它不起作用.
有任何想法吗?
模板引擎可能正在寻找一个名为的属性another_list.forloop,当然不存在.
如果您想同时循环浏览两个列表,最好的解决方案可能是zip事先在您的视图中找到它们.