Flask-wtf上传文件错误

时间:2015-05-11 18:45:00

标签: python flask wtforms flask-wtforms

关注https://github.com/lepture/flask-wtf/blob/v0.9.4/docs/form.rst 首先,我收到一条错误消息“项目合同字段中的错误 - 无”。如果我删除“validators = [FileRequired(),FileAllowed(['pdf'],'仅限Pdf!')])”部分,我收到另一条错误消息'unicode'对象没有属性'filename'

    proj_contract = FileField('Project contract'), validators=[FileRequired(), FileAllowed(['pdf'], 'Pdf only!')])

    <form method="POST" action="{{url_for('.new_project')}}" enctype="multipart/form-data"> 
        {{form.proj_contract.label}}:<input type="file" value="Upload"><br><br>
        <input type="submit" value="Next" name="Next">
    </form> 

    def new_project():
        projectInfoForm = ProjectInfoForm(request.form)
        if request.method == 'GET':
            return render_template('application/project/projectinfo.html', form = projectInfoForm)
        else:
            if projectInfoForm.validate_on_submit():
                c_filename = secure_filename(projectInfoForm.proj_contract.data.filename)
projectInfoForm.proj_contract.file.save(os.path.join(app.config['UPLOAD_FOLDER'], c_filename))

1 个答案:

答案 0 :(得分:0)

尝试更改

projectInfoForm = ProjectInfoForm(request.form)

projectInfoForm = ProjectInfoForm()

flask-wtf会自动使用request.form和request.files中的数据填充表单。如果您传递request.form,它将只处理它并且不会使用request.files填充表单。