webpy學習筆記之一

來源:互聯網
上載者:User

標籤: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學習筆記之一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.