I made a flask_wtf Form with this field:
logo_image = FileField('logo_image', validators=[FileRequired(), FileAllowed(['jpg', 'png'], 'Images only!')])
My form looks like this:
<form action="" method="POST" name="app_branding" enctype="multipart/form-data">
{{ form.csrf_token }}
{{ form.brand.label }} {{ form.brand }}
{{ form.logo_image.label }} {{ form.logo_image }}
{{ form.title_text.label }} {{ form.title_text }}
{{ form.first_paragraph.label }} {{ form.first_paragraph }}
{{ form.faq.label }} {{ form.faq }}
{{ form.privacy_policy.label }} {{ form.privacy_policy }}
{{ form.success_message.label }} {{ form.success_message }}
{{ form.submit.label }} {{ form.submit }}
</form>
For debugging, in my view, I put:
@expose('/', methods=['GET', 'POST'])
def index(self):
form = BrandForm(request.form)
print(form.validate())
print(form.errors)
print("request.files")
print(request.files)
And in the console I get the message that logo_image is required, even though it is there in request.files:
False
{'logo_image': ['This field is required.']}
request.files
ImmutableMultiDict([('logo_image', <FileStorage: u'20140725_095232.jpg' ('image/jpeg')>)])
How do I get the FileRequired() method to detect the file?
request.form
only contains form input data.request.files
contains file upload data. You need to pass the combination of both to the form. Since your form inherits from Flask-WTF'sForm
(now calledFlaskForm
), it will handle this automatically if you don't pass anything to the form.Without Flask-WTF, use a
CombinedMultiDict
to combine the data and pass that to the form.