F
F
FFFF1214512352020-02-12 15:49:40
Flask
FFFF121451235, 2020-02-12 15:49:40

Why does FileField.data return class 'str' and not class 'werkzeug.datastructures.FileStorage'?

To upload a custom image to the site (flask) use wtf forms in forms.py

class AddImage(FlaskForm):
   image = FileField('Загрузите изображение(только .png)', validators=[DataRequired()])

In main.py, when processing the form, the line data
print(type(form.image.data))
print(type(form.image))
form.image.data.save(image_name)

return
<class 'str'>
<class 'wtforms.fields.simple.FileField'>
и ошибку 'str' object has no attribute 'save'

Why is form.image.data returning a string and not the file itself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Filart97, 2020-02-12
@Filart97

You need to save the object of the orm model that you are using. The 'wtforms.fields.simple.FileField' object also most likely does not have a save method.
The logic should be like this:

profile = Profile()
image = form.image
profile.avatar = image
profile.save()

That is, the picture is not just saved, but an object of the Profile class is updated / created. Understandably?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question