Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get static files in Flask without url_for('static', file_name='xxx')

I don't want use url_for('static', file_name='foo.jpg') to get static file in template.

how to get static file in this way:

<img src="/pic/foo.jpg" />

thanks

like image 498
Robin Avatar asked Dec 04 '12 15:12

Robin


1 Answers

You can set up your own route to serve static files. Add this method and update the static path directory in the send_from_directory method, then your img tag should work.

@app.route('/pic/<path:filename>')
def send_pic(filename):
    return send_from_directory('/path/to/static/files', filename)

For a production app, you should set up your server to serve static files directly. It would be much faster and use less server resources, but for a few users the difference shouldn't be a problem.

like image 144
i_4_got Avatar answered Oct 12 '22 04:10

i_4_got