
- 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 – RESTful 應用
REST 代表 **表述性狀態轉移**。REST 是基於 Web 標準的架構,並使用 HTTP 協議進行資料通訊。它圍繞資源展開,其中每個元件都是一個資源,並且可以透過使用 HTTP 標準方法的通用介面訪問資源。REST 最初由 **Roy Fielding 於 2000 年** 引入。
什麼是 RestController
TurboGears 中的 RestController 提供了一種機制來訪問請求的方法,而不僅僅是 URL。標準 HTTP 術語包括:GET、POST、PUT 和 DELETE。RestController 支援這些,並且還添加了一些用於 URL 排程的快捷方式,這使得對於使用者來說,將資料顯示為表單和列表變得更容易。
為了解釋 REST 如何與 TurboGears 一起工作,我們將定義一個簡單的 Web 服務,該服務公開學生列表。
學生模型的程式碼如下所示:
model\student.py
# -* - coding: utf-8 -*- from sqlalchemy import * from sqlalchemy.orm import mapper, relation, relation, backref from sqlalchemy import Table, ForeignKey, Column from sqlalchemy.types import Integer, Unicode, DateTime from hello.model import DeclarativeBase, metadata, DBSession from datetime import datetime class student(DeclarativeBase): __tablename__ = 'student' uid = Column(Integer, primary_key = True) name = Column(Unicode(20), nullable = False, default = '') city = Column(Unicode(20), nullable = False, default = '') address = Column(Unicode(100), nullable = False, default = '') pincode = Column(Unicode(10), nullable = False, default = '')
現在,基於 RestController 建立一個控制器,並提供一個檢視函式以 JSON 格式列出學生列表。
Controllers\student.py
from tg import RestController from tg import expose from hello import model from hello.model import DBSession from hello.model.student import student from tg.decorators import with_trailing_slash class StudentController(RestController): @expose('json') def get_all(self): students = DBSession.query(student).all() return dict(students=students)
透過在 **root.py** 中合併以下幾行,將此 StudentController 裝載到應用程式的 RootController 中:
from hello.controllers.student import StudentController class RootController(BaseController): students = StudentController()
轉到 **https://:8080/students**,它將以 JSON 格式提供我們的學生列表。
我們使用 post 方法來定義如何將學生儲存到資料庫。每當使用 POST 請求訪問 **https://:8080/student** URL 時,都會呼叫此方法:
@expose('json') def post(self, name, city, address, pincode): newstudent = student(name = name, city = city, address = address, pincode = pincode) DBSession.add(newstudent) DBSession.flush() return dict(student = newstudent)
使用 **get_one()** 方法,我們可以將資料庫中的一個專案顯示給使用者:
@expose('json') def get_one(self, movie_id): newstudent = DBSession.query(student).get(uid) return dict(movie = movie)
PUT 是使用 REST 更新現有記錄的方法:
@expose('json') def put(self, name = name, city = city, address = address, pincode = pincode, **kw): newstudent = DBSession.query(student).get(name) newstudent.name = name newstudent.city = city newstudent.address = address newstudent.pincode = pincode return dict(student = newstudent)
delete 的主力連線到 post_delete 方法。在這裡,我們實際上從資料庫中刪除記錄,然後重定向回列表頁面:
@expose('json') def post_delete(self, uid, **kw): newstudent = DBSession.query(student).get(uid) DBSession.delete(newstudent) return dict(movie = newstudent.uid)