我正在尝试使用flask_wtforms上传文件。html页面被打开,我可以选择文件,但当我点击提交时,什么也不会发生。没有验证错误或任何其他错误。我试着在文档中搜索支持,但无济于事。以下是代码和相关导入:
路由.py
from werkzeug.utils import secure_filename
import os
@app.route('/upload', methods=['GET', 'POST'])
def upload():
form = OrderForm()
if form.validate_on_submit():
f = form.photo.data
filename = secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return render_template('upload.html', form=form)
return render_template('upload.html', form = form)
forms.py
photo = FileField('Prescription', validators=[FileRequired(), FileAllowed(['txt', 'jpg', 'jpeg', 'png'], 'Images only!')])
submit = SubmitField('Place order')
config.py
class Config(object):
UPLOAD_FOLDER = 'D:\\projects\\project1\project1\\uploads'
编辑1:已添加
upload.html
文件
upload.html
{% block content %}
<form method="POST" enctype="multipart/form-data">
{{ form.hidden_tag() }}
<p>
{{ form.prescription() }}
</p>
<p>
{{ form.submit() }}
</p>
</form>
{% endblock %}