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

折月煮酒 提交于 2019-12-12 02:12:25

问题


How to use this solution https://stackoverflow.com/a/10067749/604240 in jinja 2 template?


回答1:


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.

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()

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



回答2:


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).



来源:https://stackoverflow.com/questions/10100711/how-to-display-parent-entity-while-looping-through-child-entities-in-jinja2-temp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!