Flask:迭代字典,其值是元组列表
我有一个字典,其值是元组列表。我想为每个键建立一个表。
mydict = {'Western Division': [(0, 1, 'Oakland'), (0, 2, 'San Jose')], 'Eastern Division': [(1, 1, 'Boston'), (1, 2, 'Buffalo')]}
我的模板是:
{% for key, value in mydict %}
<table>
<tr>
<th> {{ key }} </th>
</tr>
{% for team in value %}
<tr>
<td>{{ team[2] }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
这给了我一个 ValueError:太多值无法解包(预期 2)
我尝试将第一个 for 循环更改为 for key, value, team
,认为我想调用每个元组在每个键的每个列表中,但得到相同的错误(预期 3)。
最后,我尝试在 mydict.items 中查找键、值
并得到 TypeError: 'builtin_function-or_method' object is not iterable。
我绝对有可能在创建字典时在上游犯了一个错误,但我怀疑我只是没有正确构建我的模板。
I have a dict whose values are lists of tuples. I want to build a table for every key.
mydict = {'Western Division': [(0, 1, 'Oakland'), (0, 2, 'San Jose')], 'Eastern Division': [(1, 1, 'Boston'), (1, 2, 'Buffalo')]}
My template is:
{% for key, value in mydict %}
<table>
<tr>
<th> {{ key }} </th>
</tr>
{% for team in value %}
<tr>
<td>{{ team[2] }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}
This gives me a ValueError: too many values to unpack (expected 2)
I tried changing the first for-loop to for key, value, team
, thinking that I want to call each tuple in each list in each key, but got the same error (expected 3).
Lastly, I tried for key, value in mydict.items
and got TypeError: 'builtin_function-or_method' object is not iterable.
It's definitely possible that I made a mistake further upstream in creating the dict, but I suspect I am just not building my template correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@mechanical_meat 在他们的评论中提供了答案。
items
在 jinja2 中需要括号。@mechanical_meat provided the answer in their comment.
items
needs parens in jinja2.