表单验证不正确

时间:2016-12-13 16:37:36

标签: python file-upload wtforms

我有一个函数,它为输入到我的表单中的文件类型提供验证。这是重要的部分......

if form.validate_on_submit():
        flash('Thanks %s, we will try to get back to your regarding you application as soon as possible.' % form.name.data) 
        print "Form successfully submitted"
        submit_name = form.file_upload.data.filename
        if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
            filename = secure_filename(submit_name)
            form.file_upload.data.save('uploads/' + filename)
            return redirect('home')
        else:
            flash('File %s is not an accepted format' % submit_name)

在没有文件的情况下提交表单时,我仍然收到flash消息'文件...不是可接受的格式'。但是,如果没有插入文件,我不想要flash消息。有谁知道怎么做?

1 个答案:

答案 0 :(得分:0)

需要添加:

elif submit_name == ""

然后是flash消息

flash('File %s is not an accepted format' % submit_name)

并删除else语句以解决问题。