Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 挂架使用一组表单元素进行FormEncode_Python_Pylons_Formencode_Htmlfill - Fatal编程技术网

Python 挂架使用一组表单元素进行FormEncode

Python 挂架使用一组表单元素进行FormEncode,python,pylons,formencode,htmlfill,Python,Pylons,Formencode,Htmlfill,我有一个挂架应用程序,正在使用FormEncode和HtmlFill来处理我的表单。我的模板(Mako)中有一个文本字段数组 更新 IRC建议我将元素名称从yardage[]更改为yardage 没有结果。它们都应该是int,但将f放入其中一个元素不会导致它无效。如前所述,我能够验证其他表单字段。下面是我的整个模式 import formencode class CourseForm(formencode.Schema): allow_extra_fields = True f

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

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

import formencode class 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()) 导入formencode 类课程表单(formencode.Schema): 允许\u额外\u字段=真 过滤器\额外\字段=真 name=formencode.validators.NotEmpty(消息={'empty':'name不得为空'}) PAL= FraveCopy.Frach(FraveCudial.ValueStudio.In()) 码数=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>
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())
@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!'
HoleSchema将验证
hole-#.par
hole-#.yardage
均为整数且不为空
formencode.ForEach
允许我将
HoleSchema
应用到通过将
变量\u decode=True
传递到
@validate
装饰器而获得的字典

以下是我的
提交
操作

控制器

<tr>
  <td>Yardage</td>
  % for hole in range(9):
  <td>${h.text('hole-%s.yardage'%(hole), maxlength=3, size=3)}</td>
  % endfor
</tr>
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())
@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
decorator可以更简洁地验证和填写表单。
变量_decode=True
非常重要,否则将无法正确创建字典

c.form_result = schema.to_python(request.params) - (without dict)

它似乎工作得很好。

您能再分享一些代码吗,不仅仅是控制器代码?