Flask-wtf中的append_entry FieldList不能超过一个 [英] can't append_entry FieldList in Flask-wtf more than one

查看:62
本文介绍了Flask-wtf中的append_entry FieldList不能超过一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有flask-wtf的表格,用于上传一些图像,也可以将文件字段设置为多个

I have form with flask-wtf for upload some image, also file field can be multiple

我的表格:

 class ComposeForm(Form):
     attachment = FieldList(FileField(_('file')), _('attachment'))
     add_upload = SubmitField(_('Add upload'))

我的观点:

  if form.validate_on_submit():
         if form.add_upload.data:
             form.attachment.append_entry()
             return render_template('mailbox/compose.html', form=form)
         else:
             form.attachment.append_entry()

我的模板:

<form method="POST" enctype="multipart/form-data" action=".">
                {% for field in form %}
                {{field}}
                {% endfor %}
</div>

当我在 append_entry 表单中使用 enctype ="multipart/form-data" 无效时,仅追加一个字段再次单击add_upload,但是刷新后,我只有一个字段(不是两个)

when i use enctype="multipart/form-data" in form append_entry doesn't work, only append one more field again click on add_upload but after refresh i have agen only one field ( not two )

我该如何解决?没有错误,我认为因为enctype wtform忘记了我必须添加更多:D

how i can fix this? there is no error, i think because of enctype wtform forget how many field i have to add more :D

推荐答案

您调用的 append_entry 丢失了数据.

文档:

append_entry([数据])

append_entry([data])

使用可选的默认数据创建一个新条目.

Create a new entry with optional default data.

以这种方式添加的条目将不会接收表单数据,而只能接收对象数据.

Entries added in this way will not receive formdata however, and can only receive object data.

如果您要获取在表单上提交的数据,则可以尝试使用 pop_entry .或者至少进行一些调试,然后查看 form.attachment.entries 的外观.它是否包含值?当您遍历这些值时会发生什么?

If you're trying to get the data that was submitted on the form, you might try to use pop_entry. Or at least doing some debugging and seeing what form.attachment.entries looks like. Does it contain values? What happens when you iterate through those values?

这篇关于Flask-wtf中的append_entry FieldList不能超过一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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