11

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

7
  • 3
    As long as /pic/foo.jpg exists it should work fine. Did you try it? Commented Dec 4, 2012 at 15:35
  • Just from curiosity - why don't want to use? :) Commented Dec 4, 2012 at 18:43
  • @Cfreak thank you , it's works fine. i write the wrong path before. Commented Dec 5, 2012 at 3:17
  • @IgnasB. i think common path more convenient and intuitive for FE, what's your point Commented Dec 5, 2012 at 3:26
  • @Robin, well just asked :) I thought maybe you spotted some performance issue or something. I always prefer to have dynamic things, just in case I will change static dir to another or smth, so I don't ned to worry then about the some time ago created template which I already forgot to exist :) But this is everyone's choice ;) Commented Dec 5, 2012 at 6:20

1 Answer 1

17

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.