码迷,mamicode.com
首页 > Web开发 > 详细

webpy学习笔记之一

时间:2014-10-24 14:43:31      阅读:205      评论:0      收藏:0      [点我收藏+]

标签: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.sql

CREATE 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.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中定义了数据库连接的方式,三个函数,各自功能很容易理解

/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,一个文本框,一个按钮,然后分别定义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,用户可以自己写

运行界面如图:

bubuko.com,布布扣

不要以为没事了,我测试了一下,插入英文可以正确显示,但是插入中文显示的是:??这是数据库编码问题还是模板的显示问题还没进一步弄明白,先往下做








webpy学习笔记之一

标签:des   style   blog   http   io   os   ar   for   sp   

原文地址:http://blog.csdn.net/recsysml/article/details/40424809

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!