webpy学习笔记之一
来源:互联网 发布:安卓5.0源码下载 编辑:程序博客网 时间:2024/06/04 17:49
这几天在学习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,用户可以自己写
运行界面如图:
不要以为没事了,我测试了一下,插入英文可以正确显示,但是插入中文显示的是:??这是数据库编码问题还是模板的显示问题还没进一步弄明白,先往下做
0 0
- webpy学习笔记之一
- Python学习笔记(初识webpy)
- 【webpy】webpy笔记
- 学习webpy官网示例笔记
- python网络编程学习笔记:webpy框架 (转)
- webpy
- webpy
- webpy
- 【Python】Webpy 源码学习(转)
- webpy学习之serving images
- 学习笔记之一
- C#学习笔记之一
- Jabber学习笔记 之一
- C++学习笔记之一
- ABAP学习笔记之一
- 学习笔记之一
- java学习笔记之一
- Strus学习笔记之一
- LINUX C语言中返回字符串函数
- 原创伪c代码实现线程池
- python中cursor操作数据库
- Oracle impdp 数据泵导入
- 对于MVVM大家觉得如何?过来聊聊
- webpy学习笔记之一
- ant修改文件中的内容 replace命令的使用
- Android :layer-list的用法
- Python 正则表达式提取URL中的Path
- 【Unity3D自学记录】利用代码修改图片属性(Inspector)
- 一些总结:图片太大能显示问题解决 Bitmap 、oom、Imagerloaderconfig、getclass
- Python中sort以及sorted函数初探
- 重定向输出流实现程序日志
- 如何在圆内随机点坐标