跳转到主要内容

具有嵌套表单等高级功能的表单库

项目描述

Build Status Current Release Latest Documentation Status Python Support

简介

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框架的表单循环示例。

https://github.com/Pylons/deform/raw/master/docs/example.png

示例代码

"""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添加了一些逻辑来为这些表单控件中的几个添加只读支持,如下所述。

    CheckboxWidgetCheckboxChoiceWidget

    由于复选框值处理的性质,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

  • 明确指出,对于传递给 SelectWidgetRadioChoiceWidget 的值,需要序列类型(列表、元组或 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

文档

2.0.14 (2020-08-26)

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)

2.0.11 (2020-08-21)

  • 停止支持 Python 3.4。

  • 将输入自动聚焦的实现从 JavaScript 更改为 HTML5 输入属性。

    表单现在有一个可选的 focus 参数(onoff),字段也有一个可选的 autofocus 参数。

    • 如果 focuson,并且至少有一个字段将 autofocus 规范参数设置为 on,则这些字段中的第一个将在页面加载时获得焦点。

    • 如果 focuson 或省略,并且没有字段将 autofocus 规范参数设置为 on,则页面上的第一个表单的第一个输入将在页面加载时获得焦点。

    • 如果 focusoff,则不会进行聚焦。

    默认值:on。(这与 JavaScript 实现相同。)

    请参阅 https://github.com/Pylons/deform/pull/365/

  • 将 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)

2.0.8 (2019-10-07)

2.0.7 (2018-11-14)

2.0.6 (2018-08-29)

2.0.5 (2018-02-20)

2.0.4 (2017-02-11)

  • 添加将翻译函数传递给deform.renderer.configure_zpt_renderer的能力

2.0.3 (2016-11-19)

2.0.2 (2016-11-14)

  • 修复了