gpt4 book ai didi

google-app-engine - 遍历 Jinja2 中的对象?

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:46 25 4
gpt4 key购买 nike

我在 Google App Engine 上使用 Jinja2。我有一个呈现通用模板的 ListView。目前,我不确定到底要显示什么,所以我只想显示模型的每个属性。

有没有办法迭代对象以在表格单元格中输出每个对象?

例如:

{% for record in records %}
<tr>
{% for attribute in record %}
<td>{{ attribute }}</td>
{% endfor %}
</tr>
{% endfor %}

任何建议表示赞赏。谢谢

最佳答案

在上下文中设置 getattr 是个坏主意(并且已经有内置过滤器 attr )。 Jinja2 提供 dict like访问属性。

我想你应该这样做:

{% for record in records %}
<tr>
{% for attribute in record.properties() %}
<td>{{ record[attribute] }}</td>
{% endfor %}
</tr>
{% endfor %}

这样更好...

关于google-app-engine - 遍历 Jinja2 中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10921073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com