標籤:des style blog http io os ar for sp
這幾天在學習webpy架構,之前學過一段時間,後來各種轉移架構,導致沒有學透徹,都是皮毛,各種列印hello world!汗!現在將webpy的學習過程和思路寫下來,便於複習和總結。
資料主要是webpy官方文檔,首先看了入門,然後就跟著官網的幾個例子,照貓畫虎啊
系統:ubuntu14.04
工具:VIM
例子:Todo list
沒什麼難的,主要是跟著寫和調試的過程中理解資料的傳輸串流程
######################################################################################################################################
結構如下:
/schema.sql/templates: /templates/base.html /templates/index.html/model.py/todo.py
/schema.sqlCREATE TABLE todo ( id INT AUTO_INCREMENT, title TEXT, primary key (id));
這是建立todo表,主鍵是int類型的id,還有個title是text類型,我用的是mysql,首先要進入mysql,mysql -u root -p,輸入密碼進入,建立資料庫todo,create database todo;,然後建立資料表也叫todo,方式是執行schema.sql,在mysql下,use todo;source schema.sql;,提示ok,然後desc todo;
mysql> desc todo;+-------+---------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+---------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || title | text | YES | | NULL | |+-------+---------+------+-----+---------+----------------+2 rows in set (0.01 sec)
這還是空表,查看錶內容,我們用select * from todo;查看
/model.pyimport webdb = web.database(dbn='mysql', db='todo', user='root',pw='123456')def get_todos(): return db.select('todo', order='id')def new_todo(text): db.insert('todo', title=text)def del_todo(id): db.delete('todo', where="id=$id", vars=locals()) model中定義了資料庫連接的方式,三個函數,各自功能很容易理解
/todo.py""" Basic todo list using webpy 0.3 """import webimport model### Url mappingsurls = ( '/', 'Index', '/del/(\d+)', 'Delete')### Templatesrender = web.template.render('templates', base='base')class Index: form = web.form.Form( web.form.Textbox('title', web.form.notnull, description="I need to:"), web.form.Button('Add todo'), ) def GET(self): """ Show page """ todos = model.get_todos() #調用model方法 form = self.form() return render.index(todos, form)#把todos和form傳入模板的index.html def POST(self): """ Add new entry """ form = self.form() if not form.validates():#form校正 todos = model.get_todos() return render.index(todos, form) model.new_todo(form.d.title)#把form文字框的內容添加到資料庫 raise web.seeother('/')#轉到‘/’下的頁面class Delete: def POST(self, id): """ Delete based on ID """ id = int(id) model.del_todo(id) #刪除id的title raise web.seeother('/')#轉到index中的GETif __name__ == '__main__': app = web.application(urls, globals()) app.run()urls定義了訪問不同路徑對應的處理類,render定義模板,Index中首先定義一個Form,一個文字框,一個按鈕,然後分別定義GET和POST方法
/templates/index.html$def with (todos, form) #傳入todos,form<table> <tr> <th>What to do ?</th> <th></th> </tr>$for todo in todos: #迴圈顯示todo.title <tr> <td>$todo.title</td> <td> <form action="/del/$todo.id" method="post"> <input type="submit" value="Delete"/> </form> </td> </tr> </table> <form action="" method="post">$:form.render()</form>
/templates/base.html$def with (page)<html><head> <title>Todo list</title></head><body>$:page</body></html>
頁面都在body中顯示
運行python todo.py 8090
瀏覽器中開啟:http://0.0.0.0:8090/
8090連接埠是自訂的,預設的也行,不過我的機器上預設的8000被佔了,指定了8090,使用者可以自己寫
運行介面
不要以為沒事了,我測試了一下,插入英文可以正確顯示,但是插入中文顯示的是:??這是資料庫編碼問題還是模板的顯示問題還沒進一步弄明白,先往下做
webpy學習筆記之一