I'm trying to upload a file using flask_wtforms. The html page gets opened and I can choose the file but nothing happens while I click submit. There is no validation error or error of any other kind. I've tried searching through the docs for support. The file is getting uploaded if I don't use flask_wtforms and go directly through flask and request object but that's not what I need. I need the flask_wtforms extension because I have to further add fields to this form and save the data in PostgreSQL using flask-SQLAlchemy. Here is the code along with the relevant imports:
routes.py
from app.forms import OrderForm
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
class OrderForm(FlaskForm):
photo = FileField('Prescription', validators=[FileRequired(), FileAllowed(['txt', 'jpg', 'jpeg', 'png'], 'Images only!')])
submit = SubmitField('Place order')
upload.html
{% extends 'base.html' %}
{% block content %}
<h1>Upload new File</h1>
<form method="POST" enctype= "multipart/form-data">
{{ form.hidden_tag() }}
<p>
{{ form.photo }}
{{ form.submit() }}
</p>
</form>
{% endblock %}
config.py
class Config(object):
UPLOAD_FOLDER = 'D:\projects\project1project1\uploads'
question from:
https://stackoverflow.com/questions/65839190/flask-wtf-file-not-uploading-but-also-not-giving-any-errors 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…