Cannot print data from database using the new version
I am using the latest version of web.py.
I am trying to print data from the database to a webpage. The code i use is the following
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()
the template is the following
$def with(votes)
$for vote in v开发者_JS百科otes:
<li>$vote.status</li>
and i am getting this when i run it
[<models.votes object at 0x0000000004525F28>]
Is this a bug with the new version cause in previous version it works.
I forgot to say that i am compiling my templates as stated here.
It works on my machine, running the latest web.py 0.36 with this code:
main.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>
this is the result:
- foo
I don't use web.py; but maybe it doesn't support generators/iterables in the templates as expected. Try fetching the results first by changing your line to:
votes = db.GqlQuery("SELECT * FROM votes").fetch(100)
精彩评论