0

我正在尝试在谷歌应用引擎上制作一个烧瓶应用程序,它在他们自己的页面上显示数据库条目。

这是我的一些views.py代码:

@app.route('/posts/<int:id>')
def display_post(id):
    post = Post.filter('id =', id)
    return render_template('display_post.html', post=post)

然后我的 display_posts.html

{% extends "base.html" %}

{% block content %}
<ul>
    <h1 id="">Post</h1>
    <li>
        {{ posts.title }}<br />
        {{ posts.content }}
    </li>
</ul>
{% endblock %}

现在,当我有一个 ID 为 5700305828184064 的帖子并访问此页面时,我应该会看到标题和内容:

www.url.com/posts/5700305828184064

但是我得到了这个回溯:

<type 'exceptions.AttributeError'>: type object 'Post' has no attribute 'filter'
Traceback (most recent call last):
  File "/base/data/home/apps/s~smart-cove-95709/1.384741962561717132/main.py", line 4, in <module>
    run_wsgi_app(app)
  File "/base/data/home/runtimes/python/python_lib/versions/1/google/appengine/ext/webapp/util.py", line 99, in run_wsgi_app
    run_bare_wsgi_app(add_wsgi_middleware(application))
  File "/base/data/home/runtimes/python/python_lib/versions/1/google/appengine/ext/webapp/util.py", line 117, in run_bare_wsgi_app
    result = application(env, _start_response)
  File "/base/data/home/apps/s~smart-cove-95709/1.384741962561717132/flask/app.py", line 874, in __call__
    return self.wsgi_app(environ, start_response)
  File "/base/data/home/apps/s~smart-cove-95709/1.384741962561717132/flask/app.py", line 864, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/base/data/home/apps/s~smart-cove-95709/1.384741962561717132/flask/app.py", line 861, in wsgi_app
    rv = self.dispatch_request()
  File "/base/data/home/apps/s~smart-cove-95709/1.384741962561717132/flask/app.py", line 696, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/base/data/home/apps/s~smart-cove-95709/1.384741962561717132/blog/views.py", line 25, in display_post
    post = Post.filter('id =', id)

如何显示给定 ID 条目的标题和内容?

4

1 回答 1

1

您需要先创建一个查询对象,然后对其应用过滤器。

q = Post.all()
post = q.filter("id =", id)

这是GAE 文档中关于查询的第一个示例。


此外,您的模板引用了 name posts,但您传入了 name post。适当地更改您的模板。

于 2015-06-02T20:25:33.363 回答