具有嵌套表单等高级功能的表单库
项目描述
简介
Deform是一个Python表单库,用于在服务器端生成HTML表单。它支持开箱即用的日期和时间选择小部件、富文本编辑器、动态添加和删除项的表单以及一些其他复杂用例,例如:日期和时间选择小部件、富文本编辑器、动态添加和删除项的表单和其他复杂用例。
Deform集成了Pyramid Web框架和其他几个Web框架。Deform附带Chameleon模板和Bootstrap 3样式。在底层,使用Colander模式进行序列化和验证。Peppercorn库将HTTP表单提交映射到嵌套结构。
尽管Deform在内部使用Chameleon模板,但您可以将渲染的Deform表单嵌入到任何模板语言中。
用例
Deform非常适合复杂的服务器端生成的表单。潜在用例包括
复杂的数据录入表单
管理界面
基于Python的网站,具有大量数据操作表单
不需要额外前端框架的网站
安装
使用pip和Python包安装最佳实践安装pip
pip install deform
示例
查看所有小部件示例。以下是一个使用Pyramid Web框架的表单循环示例。
示例代码
"""Self-contained Deform demo example."""
from __future__ import print_function
from pyramid.config import Configurator
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.httpexceptions import HTTPFound
import colander
import deform
class ExampleSchema(deform.schema.CSRFSchema):
name = colander.SchemaNode(
colander.String(),
title="Name")
age = colander.SchemaNode(
colander.Int(),
default=18,
title="Age",
description="Your age in years")
def mini_example(request):
"""Sample Deform form with validation."""
schema = ExampleSchema().bind(request=request)
# Create a styled button with some extra Bootstrap 3 CSS classes
process_btn = deform.form.Button(name='process', title="Process")
form = deform.form.Form(schema, buttons=(process_btn,))
# User submitted this form
if request.method == "POST":
if 'process' in request.POST:
try:
appstruct = form.validate(request.POST.items())
# Save form data from appstruct
print("Your name:", appstruct["name"])
print("Your age:", appstruct["age"])
# Thank user and take him/her to the next page
request.session.flash('Thank you for the submission.')
# Redirect to the page shows after succesful form submission
return HTTPFound("/")
except deform.exception.ValidationFailure as e:
# Render a form version where errors are visible next to the fields,
# and the submitted values are posted back
rendered_form = e.render()
else:
# Render a form with initial default values
rendered_form = form.render()
return {
# This is just rendered HTML in a string
# and can be embedded in any template language
"rendered_form": rendered_form,
}
def main(global_config, **settings):
"""pserve entry point"""
session_factory = UnencryptedCookieSessionFactoryConfig('seekrit!')
config = Configurator(settings=settings, session_factory=session_factory)
config.include('pyramid_chameleon')
deform.renderer.configure_zpt_renderer()
config.add_static_view('static_deform', 'deform:static')
config.add_route('mini_example', path='/')
config.add_view(mini_example, route_name="mini_example", renderer="templates/mini.pt")
return config.make_wsgi_app()
此示例位于deformdemo存储库中。使用pserve运行示例
pserve mini.ini --reload
状态
此库正在积极开发和维护。自2014年以来,Deform 2.x分支已在多个网站上投入生产。自动测试套件有100%的Python代码覆盖率,超过500个测试。
使用Deform的项目
社区和链接
2.0.15 (2020-12-10)
功能和修复
添加对Python 3.8和3.9的支持。
添加了一个新的基于jQuery插件selectize.js的小部件SelectizeWidget。[stevepiercy] https://github.com/Pylons/deform/issues/260
改进了某些小部件中readonly HTML属性的处理。HTML表单控件属性readonly使得元素不可变,这意味着用户不能编辑控件。当"readonly": "readonly"是传递到创建小部件时的attributes选项中的字典中的项目之一时,渲染的小部件既防止用户更改值,如果表单在提交后未通过验证,则字段值将显示出来。
readonly被大多数表单控件支持,但并非所有。Deform添加了一些逻辑来为这些表单控件中的几个添加只读支持,如下所述。
- CheckboxWidget和CheckboxChoiceWidget
由于复选框值处理的性质,readonly属性没有效果。为了实现只读行为,请传递attributes={"onclick": "return false;"}。这将使每个复选框项都渲染为内联JavaScriptonclick="return false;"
- MoneyInputWidget
输入中会显示提供的值,且不可编辑。
- RadioChoiceWidget
对于选定的值,它将在HTML中渲染一个属性作为 readonly="readonly",而对于所有其他值作为 disabled="disabled"。
- SelectWidget
仅用于单选选项的选择,选定的值将在HTML中渲染一个属性作为 readonly="readonly",而对于所有其他值作为 disabled="disabled"。通过设置 multiple=True 选项进行多选,不支持在 <select> 元素上使用 readonly 属性。对于多选,请使用 SelectizeWidget。
- SelectizeWidget
对于单选和多选,选定的值或值将渲染为已选择,而其他值不可选择。Selectize使用JavaScript来“锁定”表单控件。
- TextAreaWidget
输入中会显示提供的值,且不可编辑。
- TextInputWidget
输入中会显示提供的值,且不可编辑。
[stevepiercy] https://github.com/Pylons/deform/issues/260
可选地绕过资源注册表,并指定一个资源作为字典,其中键是资产类型("js" 或 "css"),其值是磁盘上资产路径的字符串或字符串列表。[tdamsma] https://github.com/Pylons/deform/issues/485
当且仅当提供 mask 选项时,条件性地加载 jquery.maskedinput。[tisdall, stevepiercy] https://github.com/Pylons/deform/pull/487
将日期部分小部件的 type="number" 替换为默认的 text。[stevepiercy] https://github.com/Pylons/deform/issues/442
明确指出,对于传递给 SelectWidget 或 RadioChoiceWidget 的值,需要序列类型(列表、元组或 x/range)。[stevepiercy] https://github.com/Pylons/deform/issues/385
从使用 nosetests 切换到 pytest 作为测试运行器,因为 nosetests 已不再维护。[stevepiercy] https://github.com/Pylons/deform/pull/497 https://github.com/Pylons/deform/pull/498
由于 Travis-CI 更改了其计划,不再适用于 Pylons 项目,因此从 Travis-CI 和 Appveyor 切换到 GitHub Actions。[stevepiercy] https://github.com/Pylons/deform/pull/492 https://github.com/Pylons/deform/pull/495
将 Deform 和 deformdemo 的 Docker 容器化作为运行测试的选项,并改进相关文档。[sydoluciani] https://github.com/Pylons/deform/pull/493 https://github.com/Pylons/deform/pull/494
弃用
取消对 Python 3.5 的支持。
Select2Widget 现已弃用,并将从 Deform 3.0.0 中移除,因为其 jQuery 插件 Select2 在 v4.0.0 中删除了锁定功能,其未来的发展不确定。[stevepiercy] https://github.com/Pylons/deform/issues/260
文档
为所有小部件添加任意 HTML5 属性的文档。[stevepiercy] https://github.com/Pylons/deform/issues/430
添加 Date、DateTime 和 Time 小部件的使用文档。将复杂数据加载、更改小部件或验证数据到 Colander 的文档引用添加到文档中。[stevepiercy] https://github.com/Pylons/deform/pull/466
2.0.14 (2020-08-26)
Field.validate() 在 peppercorn 解析失败时引发 ValidationFailure。请参阅 https://github.com/Pylons/deform/pull/242
将 maskMoney.js 升级到 3.1.1 并使用压缩版本。https://github.com/Pylons/deform/issues/412
2.0.13 (2020-08-25)
richtext.pt:现在 RichTextWidget 的 <textarea> 内容已进行 HTML 转义,而不再是。
注意,即使启用了 delayed_load,未转义的文本内容仍然会被渲染。然而,为了避免渲染未验证的用户输入,当重新显示包含错误的字段时,将忽略 delayed_load。请参阅 https://github.com/Pylons/deform/pull/204
这次,真正地将 select2 小部件更新到版本 4.0.13。请参阅 https://github.com/Pylons/deform/pull/459
2.0.12 (2020-08-23)
将 select2 小部件更新到版本 4.0.13。请参阅 https://github.com/Pylons/deform/pull/441
将 structure 关键字添加到自动完成模板的选项中,以停止其编码。请参阅 https://github.com/Pylons/deform/pull/445
在添加或删除到 DOM 时使用本地的 HTML 元素来处理改变事件。请参阅 https://github.com/Pylons/deform/pull/331
修复了在用 pytest 测试时导入 field.py 模块时出现的弃用警告。请参阅 https://github.com/Pylons/deform/pull/419
修复了在 2.0.11 版本中引入的 bug,该 bug 导致表单中的 focus 参数不尊重 checkboxChoice 和 radioChoice 小部件的 off 值。请参阅 https://github.com/Pylons/deform/pull/453
从 deform.js 中删除了未使用的变量 namematch。请参阅 https://github.com/Pylons/deform/pull/454
将 i18n 区域和 Bootstrap 样式添加到模板 readonly/password.pt 中。请参阅 https://github.com/Pylons/deform/pull/237
2.0.11 (2020-08-21)
停止支持 Python 3.4。
将输入自动聚焦的实现从 JavaScript 更改为 HTML5 输入属性。
表单现在有一个可选的 focus 参数(on 或 off),字段也有一个可选的 autofocus 参数。
如果 focus 是 on,并且至少有一个字段将 autofocus 规范参数设置为 on,则这些字段中的第一个将在页面加载时获得焦点。
如果 focus 是 on 或省略,并且没有字段将 autofocus 规范参数设置为 on,则页面上的第一个表单的第一个输入将在页面加载时获得焦点。
如果 focus 是 off,则不会进行聚焦。
默认值:on。(这与 JavaScript 实现相同。)
将 flake8-isort 插件替换为 plain old isort。请参阅 https://github.com/Pylons/deform/pull/427
改进了启动 Selenium webdriver 的脚本,以便在完成功能测试运行后清理 locale 目录,以防止意外提交其文件。我们还强制执行编码风格并添加了格式化器,并改进了贡献文档。请参阅 https://github.com/Pylons/deform/pull/428
更新 Bootstrap 到 3.4.1。
2.0.10 (2020-07-03)
更正了发布日期。
2.0.9 (2020-07-03)
向 schema.py 添加 CSRF 测试以通过覆盖率。请参阅 https://github.com/Pylons/deform/pull/403
修改了 DateTimeInputWidget,使其在支持 HTML5 日期/时间输入类型的浏览器中使用 HTML5 日期/时间输入类型。请参阅 https://github.com/Pylons/deform/pull/406
为 Swahili(sw_KE)区域添加了新翻译。请参阅 https://github.com/Pylons/deform/pull/409
按照 Chameleon 3.8.0 添加布尔 HTML 属性。
所有使用布尔HTML属性(selected、checked等)的复选框、单选按钮和选择框之前使用的是literal_false,并且会从渲染中丢弃任何False值作为HTML属性。在Chameleon 3.8.0版本中,literal_false已被移除,必须使用boolean_attributes来设置默认值。
2.0.8 (2019-10-07)
将HTML5属性添加到按钮中。查看https://github.com/Pylons/deform/pull/390
2.0.7 (2018-11-14)
向Select2Widget添加tags选项支持。查看https://github.com/Pylons/deform/pull/373
将Python 3.7改为使用稳定版本,3.8-dev作为允许失败版本,在Travis中。查看https://github.com/Pylons/deform/pull/377
更新Bootstrap到3.3.7。
添加对HTML5属性的支持(例如,包括placeholder和maxlength)。查看https://github.com/Pylons/deform/pull/345
2.0.6 (2018-08-29)
取消对Python 3.3的支持。查看https://github.com/Pylons/deform/pull/371
添加对Python 3.7的支持。
在DateTimeInputWidget中将date_submit和time_submit设置为可选。查看https://github.com/Pylons/deform/pull/369
取消将iso8601固定到0.1.11。查看https://github.com/Pylons/deform/pull/364
i18n清理https://github.com/Pylons/deform/pull/367和https://github.com/Pylons/deform/pull/368
2.0.5 (2018-02-20)
i18n清理https://github.com/Pylons/deform/pull/332和https://github.com/Pylons/deform/pull/363
修复Python 3.x下_FieldStorage中的bug(问题#339和#357)。
声明Python 3.6兼容性,并启用针对Python 3.6的测试(所有测试均通过,没有更改)。
关闭#333:MANIFEST.in包括文档、许可证和文本文件。
添加link按钮类型。查看https://github.com/Pylons/deform/issues/166
添加繁体中文本地化
2.0.4 (2017-02-11)
添加将翻译函数传递给deform.renderer.configure_zpt_renderer的能力
2.0.3 (2016-11-19)
在MappingWidget中添加手风琴:https://deformdemo.pylonsproject.org/mapping_accordion/
为样式表单按钮组添加CSS类deform-form-buttons:查看https://github.com/Pylons/deform/pull/308
为TextAreaCSVWidget添加更多选项:查看https://github.com/Pylons/deform/pull/309
始终使用默认CSS类渲染一个项:查看https://github.com/Pylons/deform/pull/306
更新用于日期选择器的pickdate.js库:查看https://github.com/Pylons/deform/pull/248
Widget Selenium测试套件在Python 2和3下都运行。通过一些隐式等待支持解决了许多Selenium测试难题。
2.0.2 (2016-11-14)
修复了