【发布时间】: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