如何使用 formencode 从 schema 渲染 HTML 表单?
我正在使用 formencode 在我的 Pylons 应用程序中验证和提交表单。文档说它也可以用于生成表单,但没有任何示例。我什至发现旧的主题说它可以完成,
form = HTMLForm(form_template, FormSchema)
form.render()
但对于最新版本的 formencode 它不起作用。
那么,请有人帮忙,如何使用最简单的表单架构生成 HTML?
class LoginForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
email = formencode.validators.String(not_empty=True)
password = formencode.validators.String(not_empty=True)
I'm using formencode for validating and submitting forms in my Pylons application. The documentation says that it can be used also for generating forms, but there is no any example. I even found the old topic which says it can be done with
form = HTMLForm(form_template, FormSchema)
form.render()
but for the latest version of formencode it doesn't work.
So, someone please help, how can I generate a HTML using the simplest form Schema?
class LoginForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
email = formencode.validators.String(not_empty=True)
password = formencode.validators.String(not_empty=True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Formencode 库不会为表单生成 html。
您引用的代码使用 formencode.htmlform 模块,该模块不再存在 在 1.1 版本中删除了,因为正如作者所说,这太愚蠢了。 :)
我认为您可能误解了该库的不同功能的功能,即提交失败后填充表单值,这是由 formencode.htmlfill 模块。
Formencode library doesn't generate html for forms.
The code you are referring to uses formencode.htmlform module which no longer exists as it was removed in 1.1 release because, as author said, it was dumb. :)
I think you may have mistaken that kind of functionality with different feature of this lib, namely filling form values after unsuccessful submission which is realised by formencode.htmlfill module.
zifot 关于 formencode 的说法是正确的。我只想补充一点,您应该查看 FormAlchemy 和 Sprox,这两个库是为从数据库模式生成表单而构建的。
What zifot said about formencode was spot on. I would only add that you should look at FormAlchemy and Sprox, two libraries built to generate forms from database schemas.