Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import web
- from pprint import pformat
- template = """
- <form name="main" method="post"> %s
- <input type="submit"/>
- </form>'
- """
- urls = ('/', 'index')
- app = web.application(urls, globals())
- class CheckboxList(web.form.Input):
- """CheckboxList input."""
- def __init__(self, name, args, *validators, **attrs):
- self.args = args
- super(CheckboxList, self).__init__(name, *validators, **attrs)
- def render(self):
- attrs = self.attrs.copy()
- x = '<fieldset %s>\n' % attrs
- for arg in self.args:
- chk_attrs = web.form.AttributeList()
- if isinstance(arg, (tuple, list)):
- value, desc= arg
- else:
- value, desc = arg, arg
- chk_attrs["name"] = self.name
- chk_attrs["type"] = "checkbox"
- chk_attrs["value"] = value
- if isinstance(self.value, (tuple, list)) and value in self.value:
- chk_attrs["checked"] = "checked"
- x += ' <label><input %s/> %s</label>\n' % (chk_attrs, web.net.websafe(desc))
- x += '</fieldset>\n'
- return x
- myform = web.form.Form(CheckboxList(name='chk', args=[('a', 'aa'), ('b', 'bb'), ('c', 'cc')]))
- class index:
- def GET(self):
- form = myform(dict(chk=['a','c']))
- return template % form.render()
- def POST(self):
- form = myform(web.input(chk=[]))
- return pformat(form.d)
- if __name__=="__main__":
- web.internalerror = web.debugerror
- app.run()
Add Comment
Please, Sign In to add comment