繁体   English   中英

如何在 Jinja2 模板中循环遍历子实体时显示父实体

[英]how to display parent entity while looping through child entities in Jinja2 template

如何在 jinja 2 模板中使用此解决方案https://stackoverflow.com/a/10067749/604240

我同意我的问题是由于缺乏知识而不是问题。 最终我想出了如何实现它。 基本上我不知道如何将循环从 python 代码链接到查询,因此它可用于 Jinja2 模板。

尽管正确的解决方案可能是将 map() 与回调一起使用 function https://developers.google.com/appengine/docs/python/ndb/queryclass#Query_map但我使用的是暂时适合我的临时解决方案。

query = Image.query()
query2 = query.filter(Image.is_slider == 'yes')
for item in query2:
    item.parent = item.key.parent().get()

并在模板中

{% for item in query2 %}
    <img src="{{ item.url }}=s1000" alt="{{ item.title }}" title="{{ item.title }}" />
    <h2>{{ item.title }}</h2>
    <h3>{{ item.gallery }}</h3>
    <a href="/gallery/{{ item.parent.slug }}">Go to gallery</a>
{% endfor %}

你为什么不在你的 jinja2 模板上尝试{{ item.key.parent().get().slug }} (假设 slug 是你的 Gallery 实体的属性)。

暂无
暂无

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

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