How do i link to images not in Static folder in flask

前端 未结 1 730
渐次进展
渐次进展 2020-12-06 05:00

In flask, how do I serve images that are not in the static folder?

I currently save the user uploaded photos on a directory that is outside the flask folder (On

相关标签:
1条回答
  • 2020-12-06 05:51

    You have the send_from_directory function that does what you want, what I would do is declare a constant called MEDIA_FOLDER with the path where the media files are located and then, the only thing you need to do is to call the function like this:

    from config import MEDIA_FOLDER
    
    @app.route('/uploads/<path:filename>')
    def download_file(filename):
        return send_from_directory(MEDIA_FOLDER, filename, as_attachment=True)
    

    Then, to invoke it, you just do this:

    {{ url_for('download_file', filename='dogs.jpg') }}
    

    You can have more info about this here.

    0 讨论(0)
提交回复
热议问题