python field_python – 使用FieldList和FormField

说明

在Form中的数据或formdata关键字参数中,您只需要一个与包含可迭代的FieldList子字段匹配的键的字典.该可迭代的项目反过来具有与FieldList的字段列表匹配的项目.

如果您按照下面的示例,我将获得预填充的嵌套表单.

代码

from collections import namedtuple

from wtforms import validators

from wtforms import Form

from wtforms import SelectField

from wtforms import BooleanField

from wtforms import TextField

from wtforms import FieldList

from wtforms import FormField

from webob.multidict import MultiDict

# OP's Code

class FullNameMixIn():

full_name = TextField(

'Full name', [

validators.required(message=u"Full name is required")

])

class GroupRoleForm(Form):

group =BooleanField('Group', default=False)

role = SelectField(

'Role',choices=[

("none", "----------"),

('approver', 'Approver'),

('editor', 'Editor')

])

class AdminEditUserForm(Form, FullNameMixIn):

group_roles = FieldList(FormField(GroupRoleForm))

# create some groups

Group = namedtuple('Group', ['group', 'role'])

g1 = Group('group-1', 'none')

g2 = Group('group-2', 'none')

# drop them in a dictionary

data_in={'group_roles': [g1, g2]}

# Build form

test_form = AdminEditUserForm(data=MultiDict(data_in))

# test print

print test_form.group_roles()

渲染HTML(截断)

  • Group Roles-0

    Group<

    /td>

    Role

    selected value="none">----------

    Approver

    Editor

    >

  • Group Roles-1

    Group

    h>

    Role

    ="none">----------

    Approver

    Editor

    <

    /li>

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值