学习webpy官网示例笔记
来源:互联网 发布:大卫罗兵逊生涯数据 编辑:程序博客网 时间:2024/06/05 22:54
今天在优化自动化运维脚本的过程中发现github上,很有借鉴意义的项目:https://github.com/luxiaok/SaltAdmin.git,顿时被webpy简单的语法吸引(和django相比)。
参考资料:
http://webpy.org/docs/0.3/tutorial.zh-cn
代码结构
.├── demo.py└── templates ├── db.html └── test.html
cat demo.py
# -*- coding: utf-8 -*-import webrender = web.template.render('templates/')#urls = ( '/(.*)','index')urls = ('/demo','index')urls = urls + ('/demo2','index2')urls = urls + ('/demo3','index3')urls = urls + ('/demo4/(.*)','index4')urls = urls + ('/db','db')urls = urls + ('/add','add')# 日志web.config.debug=True#http://192.168.3.106:8080/democlass index: def GET(self): return "hello,world"# http://192.168.3.106:8080/demo2?name=patrickclass index2: def GET(self): i = web.input(name=None) return render.test(i.name)#http://192.168.3.106:8080/demo3class index3: def GET(self): name = 'Patrick' return render.test(name)# http://192.168.3.106:8080/demo4/Patrickclass index4: def GET(self,name): # test is template name return render.test(name)class db: def GET(self): db = web.database(dbn='mysql', user='xxxx', pw='xxxx', db='webpy_test', host='xxxxx') todos = db.select('todo') return render.db(todos)class add: def POST(self): i = web.input() db = web.database(dbn='mysql', user='xxxx', pw='xxxxx', db='webpy_test', host='xxxxxx') n = db.insert('todo',title=i.title) raise web.seeother('/db')if __name__ == '__main__': app = web.application(urls,globals()) app.run()
cat templates/test.html
$def with(name)$if name: I just wanted to say <em>hello</em> to $name.$else: <em> hello</em>,world!
cat templates/db.html
$def with(todos)<url>$for todo in todos: <li id="t$todo.id">$todo.title</li></ul><form method="post" action="add"><p><input type="text" name="title"/> <input type="submit" value="Add"/></p></form>
启动服务
python demo.py 8080
相关内容
1,创建数据库webpy_test
2,创建表
CREATE TABLE webpy_test.todo ( id serial primary key, title text, created timestamp default now(), done boolean default false);
3,添加数据
INSERT INTO todo (title) VALUES ('Learn web.py');
0 0
- 学习webpy官网示例笔记
- webpy学习笔记之一
- Python学习笔记(初识webpy)
- 【webpy】webpy笔记
- python网络编程学习笔记:webpy框架 (转)
- webpy
- webpy
- webpy
- 【Python】Webpy 源码学习(转)
- webpy学习之serving images
- webpy使用笔记(二) session的使用
- Android学习笔记【示例代码】
- protobuf学习笔记-官方示例
- webpack2学习笔记<演示示例>
- 学习笔记-jQuery扩展示例
- webpy开发笔记01-调试模式下异常捕捉问题
- webpy session 判断登陆示例,报错attributeError: 'ThreadedDict' object has no attribute
- Guice学习笔记(一) 简单示例
- windows将nginx安装为系统服务
- n个数 找到最小的k个数 几种解法 和java实现
- jQuery选择器之元素选择器
- linux安装Yum
- GC content
- 学习webpy官网示例笔记
- SSH笔记
- shell编程——if语句 if -z -n -f -eq -ne -lt
- 开关电源的设计过程
- 反欺诈资料 搜索与整理
- HDU JAVA 1093
- Java代理探讨之动态代理
- Bloom Filter(布隆过滤器)的概念和原理
- 继承自SwipeRefreshLayout实现上拉刷新下拉加载