【问题标题】:How do I print a specific field from an App Engine Search API document in a jinja2 template?如何在 jinja2 模板中打印 App Engine Search API 文档中的特定字段?
【发布时间】:2013-05-14 05:17:57
【问题描述】:

我正在使用 Google App Engine Search API(在 Python 中)并且我希望能够在我的 jinja2 模板中打印特定的 search_document 字段。我在模板中使用此符号来尝试打印搜索文档的“评论”字段:

{{ scored_document.comment }}

我想要这个输出:

test

但是我得到了这个输出:

[search.TextField(name=u'comment', value=u'test')]

获取“评论”的的正确语法是什么?

API documentation 似乎表明您只能通过.fields 成员(一个列表对象)获得该值,但这种表示法似乎也不起作用:

{{ scored_document.fields.comment }}

我只是扩展了 Google 在github 上提供的搜索 API 示例:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <link rel="stylesheet" type="text/css" href="/static/css/main.css"/>
    <title>Search Demonstration App</title>
  </head>
  <body style="margin:20px;">

    <form action="/sign" method="post">
      <div>Search Demo</div>
      <div><textarea name="search" rows="1" cols="60"></textarea></div>
      <div><input type="submit" value="Search"/></div>
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="Comment"/></div>
    </form>

    {{number_returned}} of {{results.number_found}} comments found <p>
    {% for scored_document in results %}

      <!-- Just want **value** of comment here: -->
      {{ scored_document.comment }}

      <p>
    {% endfor %}

    <a href="{{ url }}">{{ url_linktext }}</a>

  </body>
</html>

【问题讨论】:

  • 我不想阅读所有代码,但试试这个 {{scored_document.get().message}}
  • {{scored_document.get().comment}}
  • @peterretief 我尝试了所有可能的组合,唯一对我有用的是这个,它不是最佳的:{{scored_document.fields.2.value|safe}}

标签: google-app-engine python-2.7 jinja2


【解决方案1】:

您想要该字段的“值”属性:

{{ scored_document.comment.value }}

【讨论】:

  • 谢谢,但它不起作用。什么都没有出来。我在{{scored_document.fields.5.value|safe}} 方面取得了一些成功,但最好使用名称而不是索引。
  • 格雷格的回答不起作用,因为“评论”是“字段”列表对象中的一个项目。 Niklas,您应该发布您的fields.#.value 解决方案作为您问题的答案。这是我发现的唯一有效的解决方案!
  • 如果遍历搜索结果文档列表:{{ document.field('name').value }}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 2012-09-14
  • 1970-01-01
  • 2013-05-18
  • 2019-02-09
  • 1970-01-01
相关资源
最近更新 更多