【问题标题】:Cannot print data from database using the new version无法使用新版本打印数据库中的数据
【发布时间】: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 类。

标签: google-app-engine web.py


【解决方案1】:

它可以在我的机器上运行,使用以下代码运行最新的web.py 0.36

ma​​in.py

import web
from google.appengine.ext import db

class Votes(db.Model):
  status = db.StringProperty()

urls = (
  '/', 'Index',
)

render = web.template.render('templates', base='base')

class Index:
    def GET(self):
        vote = Votes()
        vote.status ="foo"
        vote.put()

        votes = db.GqlQuery("SELECT * FROM Votes")
        return render.index(votes)

app = web.application(urls, globals())
main = app.cgirun()

templates/index.html

$def with(votes)

$for vote in votes:
    <li>$vote.status</li>

templates/base.html

$def with (content)
<html>
    <head>
    </head>
    <body>
        $:content
    </body>
</html>

这是结果:

【讨论】:

    【解决方案2】:

    我不使用 web.py;但也许它不像预期的那样支持模板中的生成器/迭代器。首先尝试通过将您的行更改为来获取结果:

    votes = db.GqlQuery("SELECT * FROM votes").fetch(100)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2017-11-28
      • 2016-06-23
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      相关资源
      最近更新 更多