gpt4 book ai didi

python - Pylons FormEncode 带有表单元素数组

转载 作者:行者123 更新时间:2023-11-28 21:30:54 24 4
gpt4 key购买 nike

我有一个 Pylons 应用程序,并使用 FormEncode 和 HtmlFill 来处理我的表单。我的模板 (Mako) 中有一组文本字段

  <tr>    <td>Yardage</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>    <td>${h.text('yardage[]', maxlength=3, size=3)}</td>  </tr>

但是,我似乎不知道如何验证这些字段。这是我的架构中的相关条目

码数 = formencode.ForEach(formencode.validators.Int())

我正在尝试验证每个字段都是 Int。但是,这些字段不会进行验证。

更新这里要求的是该 Controller 的操作代码。我知道它正在工作,因为我可以验证其他表单字段。

    def submit(self):        schema = CourseForm()        try:            c.form_result = schema.to_python(dict(request.params))        except formencode.Invalid, error:            c.form_result = error.value            c.form_errors = error.error_dict or {}            c.heading = 'Add a course'            html = render('/derived/course/add.html')            return htmlfill.render(                html,                defaults = c.form_result,                errors = c.form_errors                 )        else:            h.redirect_to(controler='course', action='view')

更新IRC 上建议我将元素名称从 yardage[] 更改为 yardage没有结果。它们都应该是整数,但将 f 放入其中一个元素不会导致其无效。正如我之前所说,我能够验证其他表单字段。下面是我的整个架构。

import formencodeclass CourseForm(formencode.Schema):    allow_extra_fields = True    filter_extra_fields = True    name = formencode.validators.NotEmpty(messages={'empty': 'Name must not be empty'})    par = formencode.ForEach(formencode.validators.Int())    yardage = formencode.ForEach(formencode.validators.Int())

最佳答案

事实证明我想做的事情不太正确。

模板:

<tr>
<td>Yardage</td>
% for hole in range(9):
<td>${h.text('hole-%s.yardage'%(hole), maxlength=3, size=3)}</td>
% endfor
</tr>

(应该在开始时将其放入循环中。)您会注意到第一个元素的名称将变为 hole-1.yardage 。然后我将使用<a href="http://www.formencode.org/en/latest/modules/variabledecode.html" rel="noreferrer noopener nofollow">FormEncode.variabledecode</a>把它变成一本字典。这是在

中完成的

架构:

import formencode

class HoleSchema(formencode.Schema):
allow_extra_fields = False
yardage = formencode.validators.Int(not_empty=True)
par = formencode.validators.Int(not_empty=True)

class CourseForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
name = formencode.validators.NotEmpty(messages={'empty': 'Name must not be empty'})
hole = formencode.ForEach(HoleSchema())

HoleSchema 将验证 hole-#.parhole-#.yardage都是整数并且不为空。 formencode.ForEach允许我申请HoleSchema到我通过的字典variable_decode=True@validate装饰器。

这是 submit我的行动

Controller :

@validate(schema=CourseForm(), form='add', post_only=False, on_get=True, 
auto_error_formatter=custom_formatter,
variable_decode=True)
def submit(self):
# Do whatever here.
return 'Submitted!'

使用@validate装饰器允许以更简洁的方式验证和填写表单。 variable_decode=True非常重要,否则字典将无法正确创建。

关于python - Pylons FormEncode 带有表单元素数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/994460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com