谷歌应用引擎上的Flask应用程序 - 显示数据库对象

2024-06-24 13:04:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在googleappengine上制作一个flask应用程序,在自己的页面上显示数据库条目。在

这是我的一点视图.py代码:

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

然后是我的展示_帖子.html在

^{pr2}$

现在,当我有一篇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的条目的标题和内容?在


Tags: appsinpyidappflaskwsgihome
1条回答
网友
1楼 · 发布于 2024-06-24 13:04:41

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

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

这是GAE docs on queries中的第一个示例。在


另外,您的模板引用了名称posts,但是您传递了名称post。适当地更改模板。在

相关问题 更多 >