简体   繁体   English

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

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

How to use this solution https://stackoverflow.com/a/10067749/604240 in jinja 2 template?如何在 jinja 2 模板中使用此解决方案https://stackoverflow.com/a/10067749/604240

I agree my question was due to lack of knowledge than problem.我同意我的问题是由于缺乏知识而不是问题。 Eventually I figured it out how to achieve it.最终我想出了如何实现它。 Basically I didn't know how to link loop from python code to query so it's available to Jinja2 template.基本上我不知道如何将循环从 python 代码链接到查询,因此它可用于 Jinja2 模板。

Although correct solution might be to use map() with callback function https://developers.google.com/appengine/docs/python/ndb/queryclass#Query_map but I am using temporary solution which is working for me for now.尽管正确的解决方案可能是将 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()

and in template并在模板中

{% 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 %}

Why don't you just try {{ item.key.parent().get().slug }} on your jinja2 template (assuming that slug is a property of your Gallery entity).你为什么不在你的 jinja2 模板上尝试{{ item.key.parent().get().slug }} (假设 slug 是你的 Gallery 实体的属性)。

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

相关问题 如何对 jinja2 进行单元测试? - How to unittest jinja2? 如何从 Google App Engine (Python) NDB/Datastore 中的子实体查询父实体? - How to query parent entity from child entity in Google App Engine (Python) NDB/Datastore? 使用附加标签扩展 BigQueryExecuteQueryOperator jinja2 - Extend BigQueryExecuteQueryOperator with additional labels using jinja2 Google App Engine DataStore - 如何以有效的方式从 Java 中子表的键中获取 select 父实体? - Google App Engine DataStore - How to select parent entities from keys of a child table in Java in an efficient way? 有没有办法将变量传递给 Jinja2 父母? - Is there a way to pass variables into Jinja2 parents? 通过Firebase实时数据库中的子节点获取父节点 - Getting parent node through child node in Firebase Realtime database 如何在父函数中获取子结构名称? - how can i get child struct name in parent func? 如何将 Jinja 函数添加到 .sqlfluff 配置 - How to add a Jinja function to .sqlfluff config Search for parent of an email - 访问子节点的父节点 - Firebase - Search for parent of an email - Access parent node of a child - Firebase Flutter:当小部件没有父/子关系时,如何从小部件 B 调用小部件 A 中的 function? - Flutter: How do I call a function in widget A from widget B when the widgets do not have a parent/child relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM