how to display parent entity while looping through

2019-08-23 23:49发布

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

2条回答
聊天终结者
2楼-- · 2019-08-24 00:31

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 %}
查看更多
趁早两清
3楼-- · 2019-08-24 00:42

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

查看更多
登录 后发表回答