【问题标题】:Flask application on google app engine - Showing database object谷歌应用引擎上的 Flask 应用程序 - 显示数据库对象
【发布时间】:2015-08-16 19:28:35
【问题描述】:

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

这是我的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 的条目的标题和内容?

【问题讨论】:

  • 您的模板中应该有post.title 而不是posts.titlecontent 也是如此)。

标签: python google-app-engine flask google-app-engine-python


【解决方案1】:

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

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

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


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多