简体   繁体   English

在Jinja2中两次迭代相同的列表?

[英]Iterate over the same list twice in Jinja2?

I'm trying to print a list of tags in two separate places, but the second time I for/in the list, it doesn't loop. 我正在尝试在两个不同的位置打印标签列表,但第二次我在/列表中,它不循环。

<ul>
# for tag in tags
    <li><a href="/my-tags/{{tag.name}}">{{tag.name}}</a></li>
# endfor
</ul>

<ul>
# for tag in tags
    <li><a href="/my-tags/{{tag.name}}">{{tag.name}}</a></li>
# endfor
</ul>

The second UL ends up empty if I put that in my template. 如果我把它放在我的模板中,第二个UL会变空。

Any ideas? 有任何想法吗?

Edit: 编辑:

This is how I'm populating the tags variable. 这就是我填充tags变量的方式。

contact_data.append({'name': 'Placeholder', 'emails': contact.emails, 'tags': [tag for tag in nt_tags.get_tags_by_taggee(contact)]})

Edit againt: 再次编辑:

Logged what I'm passing and its 记录我正在传递的内容及其内容

[<nt_tags.Tag object at 0x0000000005CAFF28>, <nt_tags.Tag object at 0x0000000005CAFFD0>]

Which looks like just a list not an iterator right? 这看起来只是一个列表而不是迭代器吗?

If tags is an iterator, then at the end of the first iteration, there won't be anything left to iterate. 如果tags是迭代器,那么在第一次迭代结束时,将不会有任何东西需要迭代。 You could materialise it to a list before passing it to your template eg: list(tags) in your context... 您可以在将其传递给模板之前将其具体eg: list(tags)在您的上下文中eg: list(tags) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM