遍历 Django 模板中的两个列表 [英] Iterating through two lists in Django templates
本文介绍了遍历 Django 模板中的两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Django 模板中进行以下列表迭代:
I want to do the below list iteration in django templates:
foo = ['foo', 'bar'];
moo = ['moo', 'loo'];
for (a, b) in zip(foo, moo):
print a, b
django 代码:
{%for a, b in zip(foo, moo)%}
{{a}}
{{b}}
{%endfor%}
尝试此操作时出现以下错误:
I get the below error when I try this:
File "/base/python_lib/versions/third_party/django-0.96/django/template/defaulttags.py", line 538, in do_for
raise TemplateSyntaxError, "'for' statements should have either four or five words: %s" % token.contents
我该如何实现?
推荐答案
有可能
{% for ab in mylist %}
{{ab.0}}
{{ab.1}}
{% endfor %}
但是您不能在 for
结构中调用 zip
.您必须首先将压缩列表存储在另一个变量中,然后对其进行迭代.
but you cannot make a call to zip
within the for
structure. You'll have to store the zipped list in another variable first, then iterate over it.
这篇关于遍历 Django 模板中的两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文