如何在Jinja2模板中遍历子实体时显示父实体 [英] how to display parent entity while looping through child entities in Jinja2 template

查看:90
本文介绍了如何在Jinja2模板中遍历子实体时显示父实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆