使用wtforms动态添加输入字段 [英] Add input fields dynamically with wtforms

查看:242
本文介绍了使用wtforms动态添加输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太清楚如何处理这件事。我希望我到达那里。



例如,我有一张桌子上充满了地址的表格。这些计数是动态的(可能是5或10或其他计数)。我想要在一页上编辑它们的可能性。

我的方法是创建一个带有wtforms的表单来编辑一个地址并将
乘以jinja2 for循环并附加到html properties name id
loop.index0 从itereation,所以我可以提取每一行数据手动
,并把它放回我的形式,当我想评估它。



因此,这个例子的表单将是:

  class AdressForm(Form):
name = TextField()

aproach看起来像下面的内容(分解为一个输入字段):

  {地址%中的%} 
{{forms.render_field(addressform.name,id =name_〜loop.index0,
name =name_〜loop.index0,value = address.name)}}
{%endfor% }

(forms.render_field只是一个宏y正确的类到wtforms的字段函数。就像他们在许多教程中使用)

所以这是行不通的,因为你不能传递名称参数手动到字段函数,因为wtforms从初始表单的variblename创建名称 html-paramter



那么是否有一种方法可以将前缀或后缀添加到要呈现的表单的名称中。
或者这是一个XY-问题,我的方法是完全错误的。

或者我自己做这件事很简单(我真的试图避免这个)

解决方案

WTForms有一个叫做 FormField 和另一个名为 FieldList 。这两个结合在一起会得到你想要的:
$ b $ pre code类AddressEntryForm(Form):
name = TextField()

类AddressForm(Form):
一个或多个地址的表单
地址= FieldList(FormField(AddressEntryForm),min_entries = 1)

要在AddressesForm中创建条目,只需使用一个词典列表:



pre $ user_addresses = [{name:First Address},
{name:Second Address}]
form = AddressesForm(addresses = user_addresses)
return render_template(edit.html,form = form)



<然后,在你的模板中,简单地遍历子表单:

  {%for address_entry_form in form.addresses%} 
{{forms.render_field(address_entry_form.name)}}
{%endfor%}

WTForms会自动为名称和ID加上正确的前缀,所以当你发布数据返回,你将能够得到 form.addresses.data 并获取更新数据的字典列表。


I'm not quite sure how approach this matter. I hope i get there.

For example I have a table full of addresses on a page. The count of these are dynamic (could be 5 or 10 or any other count). And I want the possibility to edit them on one page.

My approach was to create a Form with wtforms to edit one address and to multiply it in a jinja2 for loop and append to the html propertys name and id the loop.index0 from the itereation, so i can extract each row of data manually and put it back in my form, when I want to evaluate it.

So the Form for this example would be:

class AdressForm(Form):
    name = TextField()

so now my template aproach looks like the following (break down to one input field):

{% for address in addresses %}
    {{ forms.render_field(addressform.name, id = "name_" ~ loop.index0, 
                          name = "name_" ~ loop.index0, value = address.name) }}
{% endfor %}

(forms.render_field is just a macro to specify the right classes to the field function of wtforms. like they use in many tutorials)

So this is not working, since you can't pass the name parameter manually to the field function, since wtforms create the name html-paramter from the variblename of the intial Form.

So is there a way to add a prefix or postfix to the name of a form I want to render. Or is this a XY-Problem and my approach is totaly wrong.

or have I do it all plain by myself (I really try to avoid this)

解决方案

WTForms has a meta-field called FormField and another meta-field called FieldList. These two combined together will get you what you want:

class AddressEntryForm(Form):
    name = TextField()

class AddressesForm(Form):
    """A form for one or more addresses"""
    addresses = FieldList(FormField(AddressEntryForm), min_entries=1)

To create entries in the AddressesForm, simply use a list of dictionaries:

user_addresses = [{"name": "First Address"},
                  {"name": "Second Address"}]
form = AddressesForm(addresses=user_addresses)
return render_template("edit.html", form=form)

Then, in your template, simply loop over the sub-forms:

{% for address_entry_form in form.addresses %}
    {{ forms.render_field(address_entry_form.name) }}
{% endfor %}

WTForms will automatically prefix the names and the IDs correctly, so when you post the data back you will be able to just get form.addresses.data and get back a list of dictionaries with the updated data.

这篇关于使用wtforms动态添加输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆