这几天在学习webpy框架,之前学过一段时间,后来各种转移框架,导致没有学透彻,都是皮毛,各种打印hello world!
汗!
如今将webpy的学习过程和思路写下来,便于复习和总结。
资料主要是webpy官方文档,首先看了入门。然后就跟着官网的几个样例。照猫画虎啊
系统:ubuntu14.04
工具:VIM
样例:Todo list
没什么难的,主要是跟着写和调试的过程中理解数据的传输流程
######################################################################################################################################
结构例如以下:
/schema.sql
/templates:
    /templates/base.html
    /templates/index.html
/model.py
/todo.py/schema.sql
CREATE TABLE todo (
    id INT AUTO_INCREMENT,
    title TEXT,
    primary key (id)
);这是创建todo表。主键是int类型的id,还有个title是text类型,我用的是mysql,首先要进入mysql,mysql -u root -p。输入password进入。建立数据库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.py
import web
db = 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中定义了数据库连接的方式。三个函数。各自功能非常easy理解
/todo.py
""" Basic todo list using webpy 0.3 """
import web
import model
### Url mappings
urls = (
    '/', 'Index',
    '/del/(\d+)', 'Delete'
)
### Templates
render = 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中的GET
if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()urls定义了訪问不同路径相应的处理类,render定义模板,Index中首先定义一个Form,一个文本框,一个button,然后分别定义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/
8090port是自己定义的,默认的也行。只是我的机器上默认的8000被占了,指定了8090,用户能够自己写
执行界面如图:
不要以为没事了。我測试了一下,插入英文能够正确显示,可是插入中文显示的是:??这是数据库编码问题还是模板的显示问题还没进一步弄明确,先往下做
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号