[英]how to display parent entity while looping through child entities in Jinja2 template
我同意我的問題是由於缺乏知識而不是問題。 最終我想出了如何實現它。 基本上我不知道如何將循環從 python 代碼鏈接到查詢,因此它可用於 Jinja2 模板。
盡管正確的解決方案可能是將 map() 與回調一起使用 function https://developers.google.com/appengine/docs/python/ndb/queryclass#Query_map但我使用的是暫時適合我的臨時解決方案。
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 模板上嘗試{{ item.key.parent().get().slug }}
(假設 slug 是你的 Gallery 實體的屬性)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.