So I'm trying to make a form to upload profile image but I keep getting this error AttributeError: 'FileField' object has no attribute 'save'
when I try to upload a image.
My view:
change_info_form = ChangeInformationForm()
if change_info_form.validate_on_submit():
if change_info_form.img.data != None:
path = str(app.config['UPLOAD_FOLDER']) + str(current_user.profile_img)
if os.path.exists(path) and str(current_user.profile_img) != 'default.svg':
os.remove(path)
change_info_form.img.save(os.path.join(app.config['UPLOAD_FOLDER'], str(current_user.id)))
My form:
class ChangeInformationForm(FlaskForm):
img = FileField()
My HTML:
<img id="img-image" src="{{ url_for('static', filename='uploads/{}'.format(current_user.profile_img)) }}" style="width: 2.5em; height: 2.5em;border-radius: 50%; margin-right: 1em;" />
<label for="img_input" class="img-input-lable">Select file</label>
{{ change_info_form.img(class='input-ctl-img', id='img_input', type='file') }}
What I've tried:
change_info_form.img.save(os.path.join(app.config['UPLOAD_FOLDER'], str(current_user.id)))
change_info_form.img.file.save(os.path.join(app.config['UPLOAD_FOLDER'], str(current_user.id)))
change_info_form.img.data.save(os.path.join(app.config['UPLOAD_FOLDER'], str(current_user.id)))
I searched everywhere but I can't manage to understand the error.
Thanks in advance.