[英]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.