提交失败后,Flask-WTF Filefield值将消失 [英] Flask-WTF Filefield value disappear after unsuccessful submit

查看:426
本文介绍了提交失败后,Flask-WTF Filefield值将消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class DogForm(Form):
birthdate = DateField(u'Birthdate ',validators = [DataRequired(message = uUser correct form DMY)],format ='%d。%m。%Y')
image = FileField(u'Image of a dog',validators = [可选()])
submit = SubmitField(u'Save')

这个

  {{wtf.form_field(dogForm.birthdate)}} 
{{wtf.form_field(dogForm.image )}}
{{wtf.form_field(form.submit)}}

,如果出生日期是错误的形式,

  form.validate_on_submit()

停止验证过程并返回生日错误

查看

  form = dogForm()
if form.validate_on_submit():
add_dog(form)

return render_template dog.html,form = form)

我的问题是FileField的文件路径消失在重新加载的形式,我不能事件设置它像其他值

  form.image.data = somepath ..


解决方案

请记住设置您的enctype HTML form到multipart / form-data

 < form method =POSTenctype =multipart / form-data> ; 
....
< / form>

了解更多详情,请查看以下链接:

http://pythonhosted.org/Flask-WTF/form.html

I have form defined like this

class DogForm(Form):
    birthdate = DateField(u'Birthdate', validators=[DataRequired(message=u"User correct form D.M.Y")], format='%d.%m.%Y')
    image = FileField(u'Image of a dog', validators=[Optional()])
    submit = SubmitField(u'Save')

template like this

{{ wtf.form_field(dogForm.birthdate) }}
{{ wtf.form_field(dogForm.image) }}
{{ wtf.form_field(form.submit) }}

Upon subbmitting, if birthdate is given in wrong form,

form.validate_on_submit()

stops validating procces and gives back birthday error

view

form = dogForm()
if form.validate_on_submit():
    add_dog(form)

return render_template("dog.html", form=form)

My problem is that file path from the FileField disappears in reloaded form and i can not event set it like other values

form.image.data = "somepath.."

解决方案

Remember to set the enctype of your HTML form to multipart/form-data

<form  method="POST" enctype="multipart/form-data">
    ....
</form>

for more detail, check the following link:

http://pythonhosted.org/Flask-WTF/form.html

这篇关于提交失败后,Flask-WTF Filefield值将消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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