一个用于生成和验证HTML表单的库
项目描述
简介
pysform是一个用于简化HTML表单的渲染/处理/验证的库。
功能
基于FormEncode的验证
试图覆盖完整的HTML规范
可扩展的渲染系统()(不必使用它)
与多个WSGI框架兼容(目前支持Werkzeug)
广泛的单元测试
依赖性少:FormEncode,pysutils,WebHelpers
代码示例
使用它的样子可能如下
class MyForm(Form):
def __init__(self):
Form.__init__(self, 'myform')
el = self.add_header('input-els', 'Optional Elements')
el = self.add_button('button', 'Button', defaultval='PushMe')
el = self.add_checkbox('checkbox', 'Checkbox')
el = self.add_file('file', 'File')
el = self.add_hidden('hidden', defaultval='my hidden val')
el = self.add_image('image', 'Image', defaultval='my image val', src='images/icons/b_edit.png')
el = self.add_text('text', 'Text')
el.add_note('a note')
el.add_note('an <strong>HTML</strong> note', False)
el = self.add_text('nolabel', defaultval='No Label')
el.add_note('a note')
el = self.add_password('password', 'Password')
el = self.add_confirm('confirm', 'Confirm Password', match='password')
el.add_note('confirm characters for password field are automatically masked')
el = self.add_date('date', 'Date', defaultval=datetime.date(2009, 12, 3))
el.add_note('note the automatic conversion from datetime object')
emel = self.add_email('email', 'Email')
el = self.add_confirm('confirmeml', 'Confirm Email', match=emel)
el.add_note('note you can confirm with the name of the field or the element object')
el.add_note('when not confirming password field, characters are not masked')
el = self.add_time('time', 'Time')
el = self.add_url('url', 'URL')
options = [('1', 'one'), ('2','two')]
el = self.add_select('select', options, 'Select')
el = self.add_mselect('mselect', options, 'Multi Select')
el = self.add_textarea('textarea', 'Text Area')
el = self.add_fixed('fixed', 'Fixed', 'fixed val')
el = self.add_fixed('fixed-no-label', defaultval = 'fixed no label')
el = self.add_static('static', 'Static', 'static val')
el = self.add_static('static-no-label', defaultval='static val no label')
而视图/控制器代码可能如下
class FormTest(HtmlTemplatePage):
def prep(self):
self.form = MyForm()
def post(self):
if self.form.is_cancel():
self.assign('cancel', True)
elif self.form.is_valid():
self.assign('values', self.form.get_values())
elif self.form.is_submitted():
# form was submitted, but invalid
self.form.assign_user_errors()
self.default()
def default(self):
self.assign('form', self.form)
问题 & 评论
当前状态
代码相对稳定,但API可能在未来发生变化。
可以通过 easy_install 使用 easy_install pysform==dev 安装 pysform tip