小编Leo*_*onF的帖子

How to bind a field in __init__ function of a form

class Example_Form(Form):
    field_1 = TextAreaField()
    field_2 = TextAreaField()

    def __init__(self, type, **kwargs):
        super(Example_Form, self).__init__(**kwargs)

        if type == 'type_1':
           self.field_3 = TextAreaField()
Run Code Online (Sandbox Code Playgroud)

In some scenarios I need to dynamically add fields into the form. The field_3 added to example form turns out to be a UnboundField. I tried to bind field_3 to form in __init__ function, but it won't work.

field_3 = TextAreaField()
field_3.bind(self, 'field_3')
Run Code Online (Sandbox Code Playgroud)

How to bind field_3 to example form?

python flask wtforms flask-wtforms

1
推荐指数
1
解决办法
1670
查看次数

标签 统计

flask ×1

flask-wtforms ×1

python ×1

wtforms ×1