import web urls = ( #把所有/(.*)的网址都交给hello这个类处理,url和处理类成对出现 '/(.*)', 'hello' ) app = web.application(urls, globals()) class hello: def GET(self, name): if not name: name = 'World' return 'Hello, ' + name + '!' if __name__ == "__main__": app.run()
我就发现python的框架用起来这么简单呢
而且测试方便
http://webpy.org/src/todo-list/0.3
todo-list的例子
新手指南
http://webpy.org/tutorial3.zh-cn
另外,关于web.py模板的中文说明
http://webpy.org/docs/0.3/templetor.zh-cn
仿照写的小例子
code.py
import web urls = ( '/', 'index' ) render = web.template.render('templates/') class index: def GET(self): return render.first('world','name') if __name__ == "__main__": app = web.application(urls, globals()) app.run()
templates\first.html
$def with (name1,name2) <html><head> <title>my si </title> </head><body> Hello $name1 , hello $name2! <table> $for c in ["a", "b", "c", "d"]: <tr class="$loop.parity"> <td>$loop.index</td> <td>$c</td> </tr> </table> </body></html>
关于loop的东西还没细看,想来也是struts里面标签那一套。。。。
看到form了,不过发现官网上的代码,经常是直接copy过来不能运行的
譬如也不说说怎么import的
譬如form.Textbox的大写T竟然成了小写,害我看了半天
话又说回来,这种解析form成table的方式,还真是比较纯后台呢,现在谁还用这个table?就只有一个好处--简单
当然,偷懒者也适用,哎呀,我又想起了我半途而废的css
最简单的官网的例子
import web from web import form as form urls = ( '/', 'index' ) render = web.template.render('templates/') login = form.Form( form.Textbox('username'), form.Password('password'), form.Button('Login'), ) class index: def GET(self): #return render.first('world','name') f = login() return f.render() if __name__ == "__main__": app = web.application(urls, globals()) app.run()
用了多个属性的官网例子
import web from web import form as form urls = ( '/', 'index' ) render = web.template.render('templates/') login = form.Form( form.Textbox("firstname", form.notnull, #put validators first followed by optional attributes class_="textEntry", #gives a class name to the text box -- note the underscore pre="pre", #directly before the text box post="post", #directly after the text box description="please enter your name", #describes field, defaults to form name ("firstname") value="bob", #default value id="nameid", #specify the id ), form.Textbox('phonenumber', size="12", maxlength="12" ), ) class index: def GET(self): #return render.first('world','name') f = login() return f.render() if __name__ == "__main__": app = web.application(urls, globals()) app.run()
如何接受form的post数据,web.input和web.data
code.py
import web from web import form as form urls = ( '/', 'index', '/add','add' ) render = web.template.render('templates/') class index: def GET(self): return render.post() class add: def POST(self): print web.input()['title1'] print web.data() raise web.seeother('/') if __name__ == "__main__": app = web.application(urls, globals()) app.run()
templates/post.html
<html><head> <title>post test </title> </head><body> <form method="post" action="add"> <input type="text" name="title" /> <input type="text" name="title1" /> <input type="text" name="title2" /> <input type="text" name="title3" /> <input type="submit" value="Add" /> </form> </body></html>