本文介绍了使用Flask的FileField验证无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Flask-WTF验证表单,但是对于FileField,它验证不正确,它无法检测到文件.
I'm using Flask-WTF to validate my form, but for the FileField, it's not validating properly, it doesn't detect the file.
form.py
class addVendorForm(Form):
name = StringField('Vendor Name',
validators=[Required()],
description='Eg. Alcatel Lucent'
)
remarks = TextAreaField('Remarks', widget=TextArea())
profile = StringField('Profile', widget=TextArea())
file = FileField('Logo',validators=[
FileRequired()])
short_hand = StringField(validators=[
Required()])
notified = BooleanField()
views.py
@application.route('/memo', methods=['GET','POST'])
@application.route('/memo/<int:record_id>', methods=['GET','POST'])
@login_required
def add_memo(record_id=None):
if record_id is None:
record = Vendor()
form = addVendorForm(request.form)
else:
record = Memo.query.filter_by(id=record_id).first_or_404()
form = addMemoForm(obj=record)
if request.method == 'POST' and form.validate():
form.populate_obj(record)
db.session.add(record)
upload()
db.session.commit()
flash('<strong>Success!</strong> Database Updated.')
return redirect(url_for('add_memo'))
return render_template('add-form.html', form=form)
似乎表单对象没有保存文件.
And it seems that form object doesn't hold the file.
>>> pp(form.file.__dict__)
'data': u'',
'default': None,
'description': u'',
'errors': [],
'filters': (),
'flags': <wtforms.fields.Flags: {}>,
'id': 'file',
'label': Label('file', 'Logo'),
'meta': <wtforms.form.Meta object at 0x7f9ac40e80d0>,
'name': 'file',
'object_data': None,
'process_errors': [],
'raw_data': [],
'short_name': 'file',
'type': 'FileField',
但这在请求中.
>>> pp(request.files)
ImmutableMultiDict([('file', <FileStorage: u'cleaned data.xlsx' ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')>)])
我还可以采用哪些其他验证方法?我只需要实现FileRequired.
What other validation approach can I implement? I just need to implement FileRequired.
推荐答案
更改
form = addVendorForm(request.form)
收件人
form = addVendorForm()
这篇关于使用Flask的FileField验证无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!