我正在尝试在谷歌应用引擎上制作一个烧瓶应用程序,它在自己的页面上显示数据库条目。
这是我的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的条目的标题和内容?
答案 0 :(得分:1)
您需要先创建一个查询对象,然后将过滤器应用于该对象。
q = Post.all()
post = q.filter("id =", id)
这是GAE docs on queries中的第一个例子。
此外,您的模板引用了名称posts
,但您传递了名称post
。适当地更改模板。