我正在尝试创建Google App Engine的数据存储区和数据库模型的基本用法。我的模型对象是
class Table(db.Model):
row = db.IntegerProperty()
col = db.IntegerProperty()
date = db.DateTimeProperty(auto_now_add=True)
它将用于保存行和列的表。用户通过html提供输入,然后我保存表并绘制数据存储区中的所有表。我使用Table.all()
来获取数据存储区中的所有表,然后尝试访问它们的内容,以便我可以打印表,但出于某种原因,在for y in range(row):
中读取table.row和table.col时显然会返回一个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()
答案 0 :(得分:1)
注意:<form action="/out?%s"
不正确,只需action="/out"
。
我敢打赌你在Table
中有其他实体,所以你的循环会选择它们。使用数据存储查看器查看By kind: Table
并删除具有空行,列值的那些。
也许您只想绘制已发布的表格?改变这些行:
tables = Table.all()
for table in tables:
self.drawTable(table.row, table.col)
为:
self.drawTable(newTable.row, newTable.col)