Skip to content Skip to sidebar Skip to footer

Flask File Not Detecting On Upload

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: <

Solution 1:

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's Form (now called FlaskForm), it will handle this automatically if you don't pass anything to the form.

form = BrandForm()

if form.validate_on_submit():
    ...

Without Flask-WTF, use a CombinedMultiDict to combine the data and pass that to the form.

from werkzeug.datastructures import CombinedMultiDict

form = BrandForm(CombinedMultiDict((request.files, request.form)))

if request.method == 'POST' and form.validate():
    ...

Post a Comment for "Flask File Not Detecting On Upload"