在使用WTForms提交时调用python函数



当使用WTForms上传配置文件的图像时,我试图实现图像压缩脚本。然而,我在理解如何设置它时遇到了一点麻烦。

下面是我的代码:

class Profile(FlaskForm):
photo = FileField('Profile Picture')
submit = SubmitField('Save Changes')
def resize400(inputImg): *image compression code here*

当我运行Resize时,它会自己工作,所以我所需要的只是一个图像文件输入。

尝试在路由页面中使用validate_on_submit()函数,而不是将其嵌入到表单类中。即

class Profile(FlaskForm):
photo = FileField('Profile Picture')
submit = SubmitField('Save Changes')
路由(或视图)页:
@app.route('/uploadProfile', methods=['GET', 'POST']
def upload_profile():
form = Profile()
if form.validate_on_submit():
photo = form.photo.data
resize400(photo)
return redirect(url_for('profile'))

别忘了用

导入它
from forms import Profile