简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM