如何使用 formencode 从 schema 渲染 HTML 表单?

发布于 2024-09-08 19:54:12 字数 545 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

无妨# 2024-09-15 19:54:12

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.

等待圉鍢 2024-09-15 19:54:12

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.

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