【问题标题】:how to display parent entity while looping through child entities in Jinja2 template如何在 Jinja2 模板中循环子实体时显示父实体
【发布时间】:2012-04-11 06:04:40
【问题描述】:

如何在jinja 2模板中使用https://stackoverflow.com/a/10067749/604240这个解决方案?

【问题讨论】:

    标签: google-app-engine jinja2 app-engine-ndb


    【解决方案1】:

    我同意我的问题是由于缺乏知识而不是问题。最终我想出了如何实现它。基本上我不知道如何将循环从 python 代码链接到查询,因此它可用于 Jinja2 模板。

    虽然正确的解决方案可能是将 map() 与回调函数 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 %}
    

    【讨论】:

      【解决方案2】:

      您为什么不在您的 jinja2 模板上尝试{{ item.key.parent().get().slug }}(假设 slug 是您的 Gallery 实体的属性)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        相关资源
        最近更新 更多