我想在Django模板中进行以下列表迭代:
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%}
尝试此操作时出现以下错误:
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 %}
但您无法zip在for结构内进行调用。您必须先将压缩列表存储在另一个变量中,然后对其进行迭代。
zip
for