簡體   English   中英

如何在 Jinja2 模板中循環遍歷子實體時顯示父實體

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

如何在 jinja 2 模板中使用此解決方案https://stackoverflow.com/a/10067749/604240

我同意我的問題是由於缺乏知識而不是問題。 最終我想出了如何實現它。 基本上我不知道如何將循環從 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM