Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7577835 建立时间:2006年5月29日 |

| |
[TurboGears]一分鐘 TurboGears - 多頁面 (Multiple Pages) 講解 软件技术
lhwork 发表于 2007/2/25 13:57:19 |
1.使用超連結 (Make a Hyper link)
return """Hello , <a href="tg">World!</a>"""
在 TurboGears 中你可以直接用 return 傳 html 程式碼到網頁頁面上, 因此在 TurboGears 中你完全可以不使用樣板(Template)來建構一個網站. 注意到這邊的 "return" 使用"三重括號"來框住要傳回的資料, 在 python 中使用"三重括號"框住的內容就如 c 語言中的 "/* */" 標籤一樣代表多行內容或註解, 裡面可以允許html語法中的一般括號" "存在, 因此我們不必為了回傳資料的正確性而將內容改寫成如 <a href=\"tg\"> 的形式.
2. 建立多個頁面 Make another pages 3 def index(self):
|
|
[TurboGears]深入 TurboGears - 多頁面的階層式網頁 (Multiple pages - Hierarchy) 软件技术
lhwork 发表于 2007/2/25 13:56:16 |
TurboGears 網頁階層的有兩類, 第一類是用"參數"來對應網頁階層, 第二類是用物件樹結構(即類別實體)來對應網頁階層.
1. 參數對應結構
例如 http://localhost:8080/calcit/4/8 "4", "8"會被分別當成 calcit 方法的第一,第二個參數傳入, 作用等同於 http://localhost:8080/calcit?A=4&B=5
如果calcit 方法不存在, 即連結資料夾名稱沒有對應到方法的話, 就會被當成參數傳到最近物件的 default()中當作參數 (在本例為 TurboGears 預設建立的 Root 物件).
2. 物件樹結構
另外三種情形都是用物件樹結構的"類別實體"(instance) 來對應網頁階層. 可以實現超過一層的網頁. TurboGears 中會自動建立 |
|
[TurboGears]一分鐘 TurboGears - 處理表單參數 (form handler) 講解 软件技术
lhwork 发表于 2007/2/25 13:55:53 |
1.傳送表單 5 <form name="add" method="post" action="/calcit">
表示名稱為"add"的表單, 使用"post"的方式, 傳送到"/calcit"連結做處理.
2. 使用樣板 4 template = """ 5 <form > ... 8 </form> 9 """
在 TurboGears 中, 除了可以直接在程式中傳回網頁連結外, 同樣地也可以直接在程式中傳回整個頁面的樣板,
3. 樣版中嵌入傳回值
3 @expose(inputform ="add") ... 6 |
|
[TurboGears]深入 TurboGears - 驗證並轉換表單參數 (Validators) 講解草稿 软件技术
lhwork 发表于 2007/2/25 13:49:16 |
在 TurboGears 中可以用 "@validate()" 裝飾方法來處理輸入表單參數的轉值與驗證. 使用的好處是除了會自動確認型態的正確性之外, 還會將字串轉換成目標型態.
例如拿前一個網頁加法器作例子, 原來例子中加總結果必須先將字串轉換成目標型態 , 因此計算式如下 SUM = (int(A)+int(B)) 我們可以使用"@validate()"來預先先確認輸入資料, 並將資料轉換成預期的資料型態. 範例如下:
from turbogears.validators import * @expose(inputform ="add") @validate(validators=dict(A=validators.Int(),B=validators.Int())) def calcit(s |
|
[TurboGears]一分鐘 TurboGears - 產生表格 (DataGrid) 软件技术
lhwork 发表于 2007/2/25 13:48:43 |
表格是最常用來呈現多筆資料的方式, TurboGears 中提供 DataGrid 方法來自動產生表格. 本例以從資料表中產生簡單的足球聯賽積分(勝差)表作為範例.
第一步 假設您已經安裝好 TurboGears. 首先, 要建立一個 TurboGears 專案. 打開命令行(Command Line or shell), 進入到要建立專案的主目錄. 在主目錄下輸入
$ tg-admin quickstart
輸入之後, 會有專案建立導引如下:
Enter project name:ProjName Enter package name [ProjName]: ProjName
ProjName 是我們自己指定的專案名稱, 專案名稱可以有空格, 第二行是 package name, 也就是實際建立的目錄(資料夾)名稱, 目錄名稱中則不能有空格.
把準備 |
|
[Python]使用 nose 測試工具簡單做測試 软件技术
lhwork 发表于 2007/2/25 12:03:52 |
三步驟使用 nose 開始測試
1. 安裝或更新 nose # easy_install -U nose
沒用過 easy_install ? 你該開始試著用了!
2. 撰寫測試用例 (test case) 打開一個新的 python 檔案, 我們統一在這個新檔案裡寫測試用例
將要測試的模組匯入:
file something.py 的內容: class Hello: def __init__(self): self.template = " |
|
[TurboGears]The Web Frameworks Jam and Turbogears 软件技术
lhwork 发表于 2007/2/25 11:57:40 |
Admittedly it was a small group—eight plus me—but I had as much fun and learned as much as I think I possibly could have, so it didn't really matter. And the small group was actually so nice that I even thought of limiting the size in the future. The manageability alone was great: we could all go to lunch together, hikes were much easier to coordinate, and everyone fit easily into the house and kitchen during barbueques.
It's very probable that the next event, which I hope to hold before |
|
[TurboGears]深入觀察 TurboGears 软件技术
lhwork 发表于 2007/2/25 11:21:18 |
許多人以為 TurboGears 能跟 Django 或 Ruby on Rails 比較的地方只是因為一個分散組件, 一個集中開發.
但 TurboGears 好用的地方並不是在它堆砌了許多開源的 python web 專案(如不曾成功過的 subway),而是真正提出了一個整合這些 python web 開發資源的方法. 並提供相當具有 python 風格(pythonic) 的使用方式.
1. Decorator 形式的 Dispatcher
|
|
[Python]中文字符串的截取 软件技术
lhwork 发表于 2007/2/25 11:01:13 |
def subString(s, length): us = unicode(s, 'utf-8') gs = us.encode('gb2312') n = int(length) t = gs[:n] while True: try: &n |
|
|