金字塔框架的表单生成库
pform的Python项目详细描述
形式
简单形式示例
表单包含三个不同的子系统:基本表单属性、字段和操作。
importpformfrompyramid.httpexceptionsimportHTTPFoundclassEditForm(pform.Form):label='Edit form'fields=pform.Fieldset(pform.TextField('name',title='Name'))@pform.button('Save')defsafe_handler(self):data,errors=self.extract()iferrors:self.add_error_message(errors)self.context.name=data['name']@pform.button('Cancel')defcancel_handler(self):returnHTTPFound(location='.')
此表单呈现一个字段name和两个操作safe和cancel(提交按钮)。现在我们可以呈现此表单:
frompyramid.viewimportview_config@view_config(route_name='....',renderer='myview.jinja2')defmy_view(request):form=EditForm(some_context,request)returnform()
也可以使用窗体类作为视图:
@view_config(route_name='....')classEditForm(pform.Form):fields=...
要执行其他自定义表单初始化,只需重写方法 如果update方法返回字典,则此值将绕过模板:
classEditForm(pform.Form):defupdate(self):# custom form initializationreturn{some_data:some_value}
要使用extract方法获取表单值,此方法返回 数据和错误。数据是表单结果,错误是错误列表:
classEditForm(pform.Form):@pform.button('Save')defsave_handler(self):data,errors=self.extract()iferrors:self.add_error_message(errors)# save data...
定制
字段和表单自定义有两种方法:
- 全球定制
pform library uses player::https://github.com/fafhrd91/pform/tree/master/examples library for customization. pform libriary defines layer category form for all templates.
- 字段/表单自定义
Also it is possible to customize widget of input template for each field. You can pass ^{tt1}$ argument to to field constructor for widget customization and ^{tt2}$ argument for input generation. Both arguments should be valid pyramid renderer path. Form accepts three different templates, ^{tt3}$, ^{tt4}$ and ^{tt1}$. ^{tt3}$ is form renderer, ^{tt4}$ if form buttons renderer, ^{tt1}$ is custom field widget renderer. If field does not use custom ^{tt1}$ then form automatically sets ^{tt1}$ for each of this fields.
要求
- python 2.6+或python 3.2+
- virtualenv
许可证
pform是根据bsd许可证提供的。
更改
0.6.2(2013年1月16日)
- 修复了chrome的组合模板和选项模板
0.6.1(2012年9月1日)
- 允许从操作处理程序返回其他数据
- 将不同的上下文传递到词汇工厂 (取决于工厂参数名称:请求、内容、上下文)
0.6(2013年7月1日)
- 添加了选项字段
- basemultichoicefield.missing值默认为[]
- 在字段集数据提取过程中复制字段缺少值
- 固定形式CSRF支持
- 修正了单选字段的“选中”值
- 如果值不是有效值,则不要使用缺少的值
0.5(2012年12月21日)
- 将flat属性添加到field和fieldset, 使用平面字段集和复合字段时使用主数据 字典而不是子字典
0.4.1(2012年12月20日)
- 固定文件字段验证
0.4(2012年12月12日)
- 添加了复合字段
- 在表单中呈现str:error消息
- 删除了显示模式
- simpleterm和simplevocabulary重命名为term和词汇表
- 从u项中删除了,从u值中删除了, 改用词汇构造器
- 使用表单和字段模板的层类别
- button类现在继承自inputfield
0.3(2012年11月27日)
- 允许在表单中添加str作为错误。验证方法
- 更好的按钮操作性
- 对无效的消息进行更好的更改
- 更改了的参数顺序无效类控制器
- 为invalid类添加了子错误支持
- 将提取参数添加到按钮类,如果设置了提取,则操作 从表单中提取值,向消息中添加错误并将数据传递给处理程序
- 添加了pform.button2decorator,extract参数为true
- 在文件字段中添加了最大大小和允许的类型参数
- 不要覆盖小部件的自定义tmpl_小部件
0.2(2012年11月13日)
- 固定提交按钮模板
- 允许从表单更新或操作处理程序返回http响应
- 添加了对player.layout的支持()
0.1(2012年7月11日)
- 初始版本