python表单验证库推荐

发布于 2024-09-08 03:46:10 字数 124 浏览 8 评论 0 原文

我想要一个表单验证库,

1.将 html 生成与表单验证分开;

2.验证错误可以很容易地序列化,例如。转储为 json 对象

您会在 python Web 项目中选择什么表单验证库?

I would like a form validation library that

1.separate html generation from form validation;

2.validation errors can be easily serialized, eg. dumped as a json object

What form validation library would you choose in a python web project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴意少年 2024-09-15 03:46:10

免责声明

一般来说,我现在对 HTML 表单库有点警惕。如果您使用大型框架中的某些内容,那么您总是必须引入整个大型框架作为您的依赖项。

许多大型框架的许多子组件都声称不依赖于该框架,但我们不要欺骗自己。如果您不使用其中一个,据我所知,至少有十几个表单库,它们的功能存在很大差异。光是选择就可能会变得相当混乱。一般来说,正如 Ian Bicking 多年前所说的,我认为现在仍然如此一种适合所有人的表单库的想法是相当可笑的。事实上,我认为您可能需要三思而行,然后再决定是否真的需要一个。大多数情况下,您只需要一个表单验证库,例如 FormEncode。这实际上取决于您想如何使用它。

对我来说,因为我不使用大型框架,所以我会选择一些轻量级的、易于拾取和配置的东西,并且不会妨碍 HTML/JS/CSS 的正常使用。

END 免责声明

我尝试过 ToscaWidgetsToscaWidgetsToscaWidgets 2, Formish变形WTForms 和 FormEncode。我不得不说,它们都不是完美的。以下是我对它们的体验:

  • ToscaWidgets、ToscaWidgets 2 - 非常强大,但也非常复杂。 ToscaWidgets 2 好得多,但它仍然是 alpha ATM。设置需要相当多的忍者技巧,并且每当您需要自定义默认模板时,您的代码往往会很快膨胀。
  • Formish/Deform - 几乎与 TW 一样强大,但 Formish 现在处于休眠状态。它也与 Mako 紧密结合,因此如果您不使用 Mako,它可能不适合您。 Deform 是 Formish 的重写,但它引入了大量的 Zope 依赖项。 Chameleon 在支持 ZPT 之外的其他模板语言方面也还不够成熟。这两个库的设置也不是特别容易。
  • WTForm - 非常简单,不会妨碍您,并且在开发方面非常活跃。它远没有上面的库那么强大,但它通常可以处理您可能遇到的 80% 的用例,所以它已经足够好了。
  • FormEncode - 自 2005 年以来一直经过验证。它经过充分测试,配备最多数量的预构建验证器,支持条件验证以及数十种语言的有用错误消息。它还具有非常简单但集中的功能,可以生成预填充值和错误消息的 HTML 表单代码。它的缺点包括偶尔不直观的 API 和绝对像意大利面条的内部代码。然而,这个库非常可靠,并且非常适合所有数据验证用例,也是我经常回来的库。

截至 2012 年底,通过 Google 和 PyPI 快速搜索 Python 验证库,会得到数百个包。除去那些正在积极开发的 Django 扩展,还有十几个值得注意的扩展。似乎有一种趋势是使用 JSON-Schema 定义架构并能够通用地验证 Python 数据结构。这可能反映了服务器应用程序开发人员正在从多个渠道(RESTful API 和 HTML 表单)接受用户数据,但仍然希望只使用一个验证库。

鉴于 Python 3.3 的发布可能会引发一场大规模的迁移现有库以支持 Python 3.x 的运动(另一方面,旧库停滞不前,仅与 Python 2.x 兼容),因此明智的做法可能是选择已经支持或正在积极支持 Python 3.x 的一个。

最后,选择表单验证库时需要关注的另一个重要方面是报告有用的错误消息的能力,从长远来看,这总是需要本地化错误消息。提供您自己的错误消息的难易程度将很快决定将该库与 Web 应用程序体系结构的其余部分集成的复杂性。

有前途的后起之秀:

  • Voluptously(非常流行,非常简单的 API)
  • Kanone (受 FormEncode 启发)
  • Schemadocopt 的同一作者,非常简单的 API )

Disclaimer

Generally speaking I'm a little wary about HTML form libraries now. If you use something from a mega-framework, you invariably have to bring in the whole mega-framework as your dependency.

Many sub-components of many mega-frameworks claim to not depend on the framework but let's not kid ourselves. If you don't use one, there are at least a dozen form libraries that I know of out there with a wide range of differences in capabilities. Just the choices alone can get quite confusing. Generally speaking, as Ian Bicking says many years ago and is still true, I think the notion of one form library that suits everybody is quite ludicrous. In fact I'd argue you probably need to think twice before deciding you really need one. Chances are mostly of the time you just need a form validation library like FormEncode. It really depends on how you want to use it.

For me, since I don't use a mega-framework, I'd choose something light-weight, easy to pick up and configure, and something that doesn't get in the way of the normal usage of HTML/JS/CSS.

END Disclaimer

I've tried ToscaWidgets, ToscaWidgets 2, Formish, Deform, WTForms and FormEncode. I have to say none of them is anywhere near perfect. Here's my experience with them:

  • ToscaWidgets, ToscaWidgets 2 - Extremely powerful, but also extremely complicated. ToscaWidgets 2 is much better but it's still quite alpha ATM. It takes quite a bit of ninja skills to setup and your code tend to bloat up fairly quickly whenever you need to customize the default templates.
  • Formish/Deform - Almost as powerful as TW but Formish is dormant now. It's also quite tightly bound to Mako so if you don't use Mako, it's probably not for you. Deform is a rewrite of Formish but it brings in tons of Zope dependencies. Chameleon is also not quite there yet in terms of supporting other templating languages other then ZPT. These 2 libraries are also not particularly easy to setup.
  • WTForm - Very simple, doesn't get in your way and it's very active in terms of development. It's nowhere near as powerful as the above libraries but it generally takes care of the 80% use cases you might encounter so it's good enough.
  • FormEncode - Tried-and-true since 2005. Its well-tested, comes with the largest number of prebuilt validators, supports conditional validation, and useful error messages in dozens of languages. It also has a very simple but focused ability to generate form code in HTML prefilled with values and error messages. Its disadvantages includes a occasionally non-intuitive API and its absolutely spagetti-like internal code. However this library is quite dependable and fits very well in all the data validation use cases and it's the one I always come back to.

As of the end of 2012, a quick Google and PyPI search for a Python validation library comes back with hundreds of packages. There are a little more than a dozen notable ones, discounting those Django extensions, that are under active development. There seems to be a trend towards defining a schema using JSON-Schema and being able to generically validate Python data structures. This is likely a reflection of the server application developers' moving accepting user data from multiple channels (RESTful APIs and HTML forms), but remain wanting to use only one validation library.

Given the release of Python 3.3 will likely spark a massive movement towards porting existing libraries over to support Python 3.x (the flip side of that is seeing old libraries stagnant and remain compatible only with Python 2.x), it may be wise to choose one that already supports or is working actively to support Python 3.x.

Lastly, another great area of concern when choosing a form validation library is the ability to report useful error messages, which invariably includes the need for the localization of error messages in the long run. The ease of supplying your own error messages will quickly determine the complexity of integrating the library with the rest of your Web application architecture.

Promising up-and-comers:

池木 2024-09-15 03:46:10

我可能会选择 WTForms

I'd probably pick WTForms.

っ左 2024-09-15 03:46:10

这个主题有点老了,但我想我会无耻地插入一个我为此目的而编写的库。它并不是 HTML 表单所独有的,而是在编写时(至少部分地)考虑到了 HTML 表单。

当我命名它时,我并没有觉得很有创意,所以现在必须使用“Validator”。给你:https://github.com/wilhelm-murdoch/Validator

This topic is a bit on the old side, but I thought I'd shamelessly plug a library I've been writing for this very purpose. It's not exclusive to HTML forms, but was written with them, at least partially, in mind.

I wasn't feeling very creative when I named it, so "Validator" will have to do for now. Here you go: https://github.com/wilhelm-murdoch/Validator

农村范ル 2024-09-15 03:46:10

这取决于天气,然后取决于您使用什么类型的框架。

对于您的任务,我建议您使用 web2py-Framework,这很简单使用并且仍然“强大”。它默认具有表单验证web2py-book 是免费的),这正是您想要的:它将 html 生成与验证分开并自动执行此操作,但您可以,如果您愿意,可以定制它。

示例:

def display_form():
    form=FORM('Your name:',
              INPUT(_name='name', requires=IS_NOT_EMPTY()),
              INPUT(_type='submit'))
    if form.accepts(request.vars, session):
        response.flash = 'form accepted'
    elif form.errors:
        response.flash = 'form has errors'
    else:
        response.flash = 'please fill the form'
    return dict(form=form)

也可以序列化错误,但对于这些问题,最好在 web2py- 上询问他们组。他们非常友善并且会很快为您提供帮助。

希望有帮助!此致..

It depends wheather, and then, what type of framework you use.

For your task, I would recommend you to use the web2py-Framework, which is easy to use and still "mighty". It has form-validation by default (the web2py-book is free), that does exactly what you want: It sepereates the html generation from the validation and does this automatically, but you can, if you wish, customize it.

An example:

def display_form():
    form=FORM('Your name:',
              INPUT(_name='name', requires=IS_NOT_EMPTY()),
              INPUT(_type='submit'))
    if form.accepts(request.vars, session):
        response.flash = 'form accepted'
    elif form.errors:
        response.flash = 'form has errors'
    else:
        response.flash = 'please fill the form'
    return dict(form=form)

It's also possible to serialize errors, but for those questions it's the best to ask them on the web2py-group. They're very nice and will help you very fast.

Hope it helps! Best regards..

属性 2024-09-15 03:46:10

这取决于您使用的底层框架。

对于 django 来说,内置表单框架是最好的,

而 kay 使用 zine 表单系统的扩展版本

,tipfy 使用 WTForms。

django 的内置系统是迄今为止最好的。

你在后台使用什么框架?

it depends on what underlying framework you use.

for django , built in form framework is best,

while kay uses extended version of zine's form system

and tipfy uses WTForms.

django's built in system is best so far .

what framework do you use under the hood ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文