【发布时间】:2011-10-12 23:17:33
【问题描述】:
我正在使用最新版本的 web.py。
我正在尝试将数据从数据库打印到网页。 我使用的代码如下
import web
from google.appengine.ext import db
from models import *
urls = (
'/', 'index',
)
render = web.template.render('templates', base='base')
class index:
def GET(self):
votes = db.GqlQuery("SELECT * FROM votes")
return render.index(votes)
app = web.application(urls, globals())
main = app.cgirun()
模板如下
$def with(votes)
$for vote in votes:
<li>$vote.status</li>
当我运行它时我得到了这个
[<models.votes object at 0x0000000004525F28>]
这是新版本的错误,因为在以前的版本中它可以工作。
我忘了说我正在按照here 的说明编译我的模板。
【问题讨论】:
-
您确定您没有使用
votes模型类隐藏votes查询结果吗?尝试将您的类定义从votes重命名为Votes。 -
是的,我确定。我将我的课程重命名为
Votes,我得到了相同的输出。 -
使用
votes = Votes.all()解决问题? -
我应该在哪里使用它?
-
将
votes = db.GqlQuery("SELECT * FROM votes")替换为votes = Votes.all()并试一试。我也在使用 web.py 最新版本,但我完全没有问题,但我倾向于使用类模型方法而不是 GqlQuery 类。