- TurboGears 教程
- TurboGears - 首頁
- TurboGears - 概述
- TurboGears - 環境
- TurboGears - 第一個程式
- TurboGears - 依賴項
- TurboGears - 服務模板
- TurboGears - HTTP 方法
- Genshi 模板語言
- TurboGears - 包含
- TurboGears - JSON 渲染
- TurboGears - URL 層次結構
- TurboGears - Toscawidgets 表單
- TurboGears - 驗證
- TurboGears - 快閃記憶體訊息
- TurboGears - Cookie 和會話
- TurboGears - 快取
- TurboGears - Sqlalchemy
- TurboGears - 建立模型
- TurboGears - CRUD 操作
- TurboGears - 資料網格
- TurboGears - 分頁
- TurboGears - 管理員訪問
- 授權與認證
- TurboGears - 使用 MongoDB
- TurboGears – 腳手架
- TurboGears - 鉤子
- TurboGears - 編寫擴充套件
- TurboGears - 可插拔應用程式
- TurboGears - RESTful 應用程式
- TurboGears - 部署
- TurboGears 有用資源
- TurboGears - 快速指南
- TurboGears - 有用資源
- TurboGears - 討論
TurboGears – 腳手架
Gearbox 工具包包含 scaffold 命令,該命令對於快速建立 TurboGears 應用程式的新元件非常有用。由 gearbox 的 quickstart 命令生成的應用程式在 model 資料夾(model.py.template)、templates 資料夾(template.html.template)和 controllers 資料夾(controller.py.template)中都有一個骨架模板。這些“.template”檔案用作建立應用程式新腳手架的基礎。
例如,要建立一個名為 mymodel 的新模型,只需執行以下命令:
gearbox scaffold model mymodel
此命令將生成 model/mymodel.py,其中定義了 newmodel 類。
# -*- coding: utf-8 -*-
"""Mymodel model module."""
from sqlalchemy import *
from sqlalchemy import Table, ForeignKey, Column
from sqlalchemy.types import Integer, Unicode, DateTime, LargeBinary
from sqlalchemy.orm import relationship, backref
from hello.model import DeclarativeBase, metadata, DBSession
class Mymodel(DeclarativeBase):
__tablename__ = 'mymodels'
uid = Column(Integer, primary_key = True)
data = Column(Unicode(255), nullable = False)
user_id = Column(Integer, ForeignKey('tg_user.user_id'), index = True)
user = relationship('User', uselist = False,
backref = backref('mymodels',cascade = 'all, delete-orphan'))
__all__ = ['Mymodel']
使用者現在可以根據需要修改表結構,然後將其匯入 model/__init__.py 以使模型在應用程式中可用。
為了建立一個模型、一個處理它的控制器類和一個索引頁面,這三個元件可以透過以下命令同時建立。
gearbox scaffold model controller template mymodel
此命令將生成 controllers\mymodel.py,其中已正確定義了 MymodelController 類。
# -*- coding: utf-8 -*-
"""Mymodel controller module"""
from tg import expose, redirect, validate, flash, url
# from tg.i18n import ugettext as _
# from tg import predicates
from hello.lib.base import BaseController
# from hello.model import DBSession
class MymodelController(BaseController):
# Uncomment this line if your controller requires an authenticated user
# allow_only = predicates.not_anonymous()
@expose('hello.templates.mymodel')
def index(self, **kw):
return dict(page = 'mymodel-index')
要開始使用此控制器,請將其安裝在應用程式 RootController 中以定義 MymodelController 的例項。在 controllers\root.py 中新增以下行:
From hello.controller.mymodel import MymodelController class RootController(BaseController): mymodel = MymodelController()
還將在 templates 資料夾中建立一個模板腳手架 templates\mymodel.html。它將充當“/mymodel”URL 的索引頁面。
在 templates 資料夾中生成的 mymodel.html 檔案如下所示:
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:py = "http://genshi.edgewall.org/"
xmlns:xi = "http://www.w3.org/2001/XInclude">
<xi:include href = "master.html" />
<head>
<title>Mymodel</title>
</head>
<body>
<div class = "row">
<div class = "col-md-12">
<h2>Mymodel</h2>
<p>Template page for Mymodel</p>
</div>
</div>
</body>
</html>
廣告