Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

python - Flask-wtf file not uploading but also not giving any errors

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...