L
L
LegitPlayer2020-05-15 01:19:51
Flask
LegitPlayer, 2020-05-15 01:19:51

FLASK | Why do I get a 404 error after uploading a file?

I upload a file, the download information is displayed in the console, the file is uploaded - I get a 404 error.

<html>
<head>
  <title>Upload</title>
</head>
<body>
<form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}">
    <input type=file name=photo>
    <input type="submit">
</form>
</body>
</html>


from flask import Flask, render_template, request
from flask.ext.uploads import UploadSet, configure_uploads, IMAGES

app = Flask(__name__)

photos = UploadSet('photos', IMAGES)

app.config['UPLOADED_PHOTOS_DEST'] = 'static/img'
configure_uploads(app, photos)

@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        filename = photos.save(request.files['photo'])
        return filename
    return render_template('upload.html')


if __name__ == '__main__':
  app.run(debug=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
myxomor469, 2020-05-18
@myxomor469

remove action="{{ url_for('upload') }}"so that after loading it does not look for a new path, but simply receives a response with the file name in this form, return str(filename)
or remove the response altogether after saving the file and let the page reload

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question