我試圖創建Google App Engine的數據存儲和db模型的基本用法。我的模型對象是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()
檢查表中現有的實體。聽起來你至少有一個(可能是之前創建的)沒有'row'值。您可能需要在行和列屬性上設置'required = True'。 –
我該怎麼做纔是db.IntegerProperty(required = True)? – EasilyBaffled
是的,沒錯。 –