Web.py Form库文档 和 示例代码 :http://webpy.org/form
参考 http://blog.csdn.net/freeking101/article/details/76148434 这篇文章改写成 Web.py 搭建测试网站
先看 官网一个使用 Form 表单的示例(code.py):
1 import web 2 from web import form 3 4 render = web.template.render(‘templates/‘) 5 6 urls = (‘/‘, ‘index‘) 7 app = web.application(urls, globals()) 8 9 myform = form.Form( 10 form.Textbox("boe"), 11 form.Textbox("bax", 12 form.notnull, 13 form.regexp(‘\d+‘, ‘Must be a digit‘), 14 form.Validator(‘Must be more than 5‘, lambda x:int(x)>5)), 15 form.Textarea(‘moe‘), 16 form.Checkbox(‘curly‘), 17 form.Dropdown(‘french‘, [‘mustard‘, ‘fries‘, ‘wine‘])) 18 19 class index: 20 def GET(self): 21 form = myform() 22 # make sure you create a copy of the form by calling it (line above) 23 # Otherwise changes will appear globally 24 print(form.render()) 25 return render.formtest(form) 26 27 def POST(self): 28 form = myform() 29 if not form.validates(): 30 print(form.render()) 31 return render.formtest(form) 32 else: 33 # form.d.boe and form[‘boe‘].value are equivalent ways of 34 # extracting the validated arguments from the form. 35 return "Grrreat success! boe: %s, bax: %s" % (form.d.boe, form[‘bax‘].value) 36 37 if __name__=="__main__": 38 web.internalerror = web.debugerror 39 app.run()
formtest.html 代码如下:
1 $def with (form) 2 3 <div align="center"> 4 <form name="main" method="post"> 5 $if not form.valid: <p class="error">Try again, AmeriCAN:</p> 6 $:form.render() 7 <input type="submit" /> 8 </form> 9 <div>