【问题标题】:Calling a python function when submitting using WTForms使用 WTForms 提交时调用 python 函数
【发布时间】:2021-12-09 21:08:06
【问题描述】:

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

这是我的代码:

    class Profile(FlaskForm):

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

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

【问题讨论】:

    标签: python flask web jinja2 flask-wtforms


    【解决方案1】:

    尝试在路由页面中使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多