url_for: how to look into another folder other tha

2019-07-04 22:39发布

问题:

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?

回答1:

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)


回答2:

I think (I can't test here right now) you can do

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

However, since CSS files are also static, I would make a subdirectory css under static, and then do

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