我正在尝试创建 Google App Engine 的数据存储和数据库模型的基本用途。我的模型对象是
class Table(db.Model):
row = db.IntegerProperty()
col = db.IntegerProperty()
date = db.DateTimeProperty(auto_now_add=True)
它将用于仅保存行和列的表。用户通过 html 提供输入,然后我保存表格并绘制数据存储区中的所有表格。我曾经Table.all()
从数据存储中获取所有表,然后尝试访问它们的内容,以便我可以打印表,但是由于某种原因,当 table.row 和 table.col 被读入时for y in range(row):
显然会返回一个 noneType,有人知道为什么吗?
import webapp2
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from logging import error
INITIAL_INPUT = """\
<html>
<body>
<form action="/out?%s" method="POST">
<input type="text" name="row" />
<input type="text" name="col" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
"""
class Table(db.Model):
"""Models an individual Guestbook entry with author, content, and date."""
row = db.IntegerProperty()
col = db.IntegerProperty()
date = db.DateTimeProperty(auto_now_add=True)
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.write(INITIAL_INPUT)
class Out(webapp2.RequestHandler):
def post(self):
newRow = self.request.POST['row']
newCol = self.request.POST['col']
newTable = Table()
newTable.row = int(newRow) if newRow else 1
newTable.col = int(newCol) if newCol else 1
newTable.put()
tables = Table.all()
for table in tables:
self.drawTable(table.row, table.col)
def drawTable(self, row , col):
write = self.response.write
write("<html><body><table>")
for y in range(row):
write("<tr>")
for x in range(col):
cell = "<td bgcolor=\"#00FF00\">" + str(x) + " " + str(y) + "</td>"
if x % 2 == 0:
cell = "<td bgcolor=\"#FF0000\">" + str(x) + " " + str(y) + "</td>"
write(cell)
write("</tr>")
write("</table></body></html>")
application = webapp2.WSGIApplication([
('/', MainPage),
('/out', Out)]
, debug=True)
def main(*args, **kwds):
run_wsgi_app(application)
if __name__ == "__main__":
main()