龙空技术网

Python 开箱即用的 HTML 表单生成库 —— Deform

MikoCody 1425

前言:

此时我们对“表单框css样式代码”都比较关心,各位老铁们都需要分析一些“表单框css样式代码”的相关内容。那么小编同时在网摘上网罗了一些关于“表单框css样式代码””的相关知识,希望小伙伴们能喜欢,小伙伴们快快来学习一下吧!

Deform 是一个用于创建 Web 表单的 Python 库,提供了许多用于处理表单数据及其验证和呈现的功能。 deform 帮助程序员创建简单或复杂的 Web 表单,并使其易于与 Pyramid、Django 和 Flask 等 Python Web 框架集成。

此外,deform 还提供了许多支持组件,如日期选择器、下拉列表、多选框、单选按钮等,这些组件可以帮助开发人员快速创建各种类型的 Web 表单。

安装

使用 pip 进行安装:

pip install deform
示例代码
"""Self-contained Deform demo example."""from __future__ import print_functionfrom pyramid.config import Configuratorfrom pyramid.session import UnencryptedCookieSessionFactoryConfigfrom pyramid.httpexceptions import HTTPFoundimport colanderimport deformclass 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()

总体而言,如果你需要在 Python Web 应用程序中处理表单,Deform 是一个非常好用的库。它提供了许多有用的功能,并且易于使用和集成到现有的 Web 框架中。

演示地址:

Github 地址:

标签: #表单框css样式代码 #pythonweb表单 #表单python