6

I have a file test.css in a folder called css. I want to create url for this file. I know that I can use url_for like

url_for('static', filename="test.css")

to create url like static/test.css but I am not able to use like

url_for('css', filename="test.css")

to create the url that I am interested in css/test.css

How can I do this?

davidism
  • 121,510
  • 29
  • 395
  • 339
ganesshkumar
  • 1,317
  • 1
  • 16
  • 35

1 Answers1

9

static just endpoint, see route and view. So you can create own endpoints:

app.add_url_rule('/css/<path:filename>', endpoint='css',
                 view_func=app.send_static_file)
tbicr
  • 24,790
  • 12
  • 81
  • 106
  • 1
    This works, just make sure that the production web server is also set up to serve the `/css` directory, just like you would have it serve the `/static` directory. – Mark Hildreth Aug 15 '13 at 16:15
  • I think, the links no more provides the intended lines. @tbicr –  Jul 15 '17 at 11:53
  • 1
    It does generate the correct url but when I run the app, it can't find the files. For example url_for('css', filename="test.css") generates "/css/test.css" which is correct but in console output it shows that the app can't find "/css/test.css". What can be the issue? – Behroz Jan 18 '21 at 17:28