TurboGears – DataGrid

ToscaWidgetsには、データを表形式ですばやく表示する方法を提供するDataGridコントロールが含まれています。DataGridオブジェクトは次のように宣言されます-

from tw2.forms import DataGrid
student_grid = DataGrid(fields = [('Name', 'name'),('City', 'city'),
   ('Address','address'), ('PINCODE', 'pincode')])

これで、showgrid()関数は、学生テーブル内のすべてのレコードを取得し、データをgrid.htmlテンプレートに公開します。最初にshowgrid()関数のコード、次にgrid.htmlコードを以下に示します-

showgrid()

@expose('hello.templates.grid')
def showgrid(self):
   data = DBSession.query(student).all()
   return dict(page = 'grid', grid = student_grid, data = data)

grid.html

<!DOCTYPE html>
<html xmlns = "http://www.w3.org/1999/xhtml"
   xmlns:py = "http://genshi.edgewall.org/"
   lang = "en">
   
   <head>
      <title>Student Registration Form</title>
   </head>
   
   <body>
      <div id = "getting_started">
         <div>${grid.display(value = data)}</div>
      </div>
   </body>

</html>

以下の表形式のデータは、次の場合に表示されます。 http://localhost:8080/showlist ブラウザにURLを入力-