python - Link to Flask static files with url_for -


how use url_for in flask reference file in folder? example, have static files in static folder, of may in subfolders such static/bootstrap.

when try serve file static/bootstrap, error.

 <link rel=stylesheet type=text/css href="{{ url_for('static/bootstrap', filename='bootstrap.min.css') }}"> 

i can reference files aren't in subfolders this, works.

 <link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.min.css') }}"> 

what correct way reference static files url_for? how use url_for generate urls static files @ level?

you have default static endpoint static files. flask application has following arguments:

static_url_path: can used specify different path static files on web. defaults name of static_folder folder.

static_folder: folder static files should served @ static_url_path. defaults 'static' folder in root path of application.

it means filename argument take relative path file in static_folder , convert relative path combined static_url_default:

url_for('static', filename='path/to/file') 

will convert file path static_folder/path/to/file url path static_url_default/path/to/file.

so if want files static/bootstrap folder use code:

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='bootstrap/bootstrap.min.css') }}"> 

which converted (using default settings):

<link rel="stylesheet" type="text/css" href="static/bootstrap/bootstrap.min.css"> 

also @ url_for documentation.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -