python表单验证库的推荐 [英] Recommendation for python form validation library

查看:31
本文介绍了python表单验证库的推荐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个表单验证库

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

1.separate html generation from form validation;

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

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

您会在 Python Web 项目中选择哪种表单验证库?

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

推荐答案

免责声明

总的来说,我现在对 HTML 表单库有点担心.如果您使用大型框架中的某些内容,则总是必须引入整个大型框架作为您的依赖项.

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.

许多大型框架的许多子组件声称不依赖于框架,但我们不要自欺欺人.如果你不使用一个,我知道至少有十几个表单库,它们的功能差异很大.仅凭选择就会变得非常混乱.一般来说,正如 Ian Bicking 多年前所说 并且现在仍然如此,我认为一种适合所有人的表单库的概念是非常荒谬的.事实上,我认为在决定你真的需要一个之前你可能需要三思而后行.大多数情况下,您只需要一个表单验证库,例如 FormEncode.这真的取决于你想如何使用它.

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.

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

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.

结束免责声明

我尝试过 ToscaWidgetsToscaWidgets 2Formish变形WTForms 和 FormEncode.我不得不说,没有一个是近乎完美的.这是我对他们的体验:

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 - 非常强大,但也非常复杂.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 表单)接受用户数据,但仍然希望只使用一个验证库.

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.

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

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.

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

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.

有前途的后起之秀:

  • Voluptuous (Very popular, very simple API)
  • Kanone (Inspired by FormEncode)
  • Schema (Same author of docopt, very simple API)

这篇关于python表单验证库的推荐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆