通过Django模板中的两个列表进行迭代 [英] Iterating through two lists in Django templates

查看:95
本文介绍了通过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 %}

,但您不能拨打中结构中的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆