How to pass uploaded image to template.html in Flask

前端 未结 2 1816
旧巷少年郎
旧巷少年郎 2020-12-04 17:03

I am using flask, and trying to do something very simple using the quickstart tutorial, just running on my machine (local server). I produce a simple upload form which succe

2条回答
  •  感情败类
    2020-12-04 17:39

    What's happening now is that /uploads/foo.jpg returns the HTML inside template.html. There you try to use /uploads/foo.jpg as the source of the img tag. Nowhere you serve the actual image out.

    Let's modify it like this: /show/foo.jpg returns the HTML page and and /uploads/foo.jpg returns the image. Replace the latter route with these two and you should be good to go:

    @app.route('/show/')
    def uploaded_file(filename):
        filename = 'http://127.0.0.1:5000/uploads/' + filename
        return render_template('template.html', filename=filename)
    
    @app.route('/uploads/')
    def send_file(filename):
        return send_from_directory(UPLOAD_FOLDER, filename)
    

提交回复
热议问题