2013-06-11 56 views
0

我試圖創建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() 
+1

檢查表中現有的實體。聽起來你至少有一個(可能是之前創建的)沒有'row'值。您可能需要在行和列屬性上設置'required = True'。 –

+0

我該怎麼做纔是db.IntegerProperty(required = True)? – EasilyBaffled

+0

是的,沒錯。 –

回答

1

注:<form action="/out?%s"不正確,它僅需要action="/out"

我敢打賭,你在Table有其他實體,所以你的循環接他們。使用數據存儲查看器查看By kind: Table並刪除具有空行col值的列。

也許你只想繪製張貼的表格?更改這些行:

tables = Table.all() 
    for table in tables: 
     self.drawTable(table.row, table.col) 

到:

self.drawTable(newTable.row, newTable.col) 
+0

爲什麼'/ out?%s'錯了?老實說,我從一個例子中複製了它,並沒有專注於它。另外,我確實想要打印所有的表格。最後,我如何訪問數據存儲查看器? – EasilyBaffled

+1

通常,使用%運算符時,字符串中的「%s」是另一個字符串的佔位符。如果你想進行自定義操作,那麼這是正確的。由於您的應用程序使用'/ out'和POST方法,因此忽略(?%s)(幸運的是)。 –

+1

要訪問數據存儲查看器,請轉到https://appengine.google.com/並選擇您的應用程序。當您看到儀表板時,單擊左側菜單欄上的數據存儲查看器鏈接。 –

相關問題