在 Jinja2 模板中循环子实体时如何显示父实体 [英] how to display parent entity while looping through child entities in Jinja2 template
问题描述
如何在 jinja 2 模板中使用此解决方案 https://stackoverflow.com/a/10067749/604240?
How to use this solution https://stackoverflow.com/a/10067749/604240 in jinja 2 template?
推荐答案
我同意我的问题是由于缺乏知识而不是问题.最终我想出了如何实现它.基本上我不知道如何将循环从 python 代码链接到查询,因此它可用于 Jinja2 模板.
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.
虽然正确的解决方案可能是使用带有回调函数的 map() https://developers.google.com/appengine/docs/python/ndb/queryclass#Query_map 但我正在使用目前对我有用的临时解决方案.
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.
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 模板中循环子实体时如何显示父实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!