首页 > Web开发 > 详细

使用 Web.py 搭建一个测试网站案例

时间:2020-05-25 17:52:46      阅读:66      评论:0      收藏:0      [点我收藏+]

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>

 

使用 Web.py 搭建一个测试网站案例

原文:https://www.cnblogs.com/chidao/p/12958116.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!