jonprasetyo
jonprasetyo

Reputation: 3586

Flask - Uploaded image is not showing - gives 404

Apologies for a noob question, also been looking for this answer for awhile now. So I have read the docs http://flask.pocoo.org/docs/patterns/fileuploads/ and I followed everything.

I have successfully uploaded the image and the image currently lives in the folder below:

/myfirstflask
    /myapp
        /static
        /templates
        routes.py
        ...
    /uploads
        myimage.jpg

Cool, so now I am trying to retrieve the image by implementing this into my routes:

@app.route('/media/<path:filename>')
def media(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

For your info the app.config['UPLOAD_FOLDER'] is 'uploads/' And within one of my rendered html templates I have this:

<img src="{{url_for('media', filename = 'myimage.jpg')}}"/>

The image returns a 404 not found, what am I doing wrong?

I then experimented, I copied the entire 'uploads' folder and put it into the 'myapp' folder and it works! But when I deleted the 'uploads' folder outside 'myapp' folder, it returns an error 404. It seems it needs to upload folders to be present... which seems to be weird, and not right.

Upvotes: 2

Views: 1059

Answers (1)

Ankush Shah
Ankush Shah

Reputation: 958

The "uploads/" folder should be in the root directory of your flask app which in your case is "/myapp".

Upvotes: 1

Related Questions