Link to Flask static files with url_for

后端 未结 2 511
渐次进展
渐次进展 2020-11-21 07:14

How do you use url_for in Flask to reference a file in a folder? For example, I have some static files in the static folder, some of which may be

2条回答
  •  醉酒成梦
    2020-11-21 07:37

    You have by default the static endpoint for static files. Also Flask application has the following arguments:

    static_url_path: can be used to specify a different path for the static files on the web. Defaults to the name of the static_folder folder.

    static_folder: the folder with static files that should be served at static_url_path. Defaults to the 'static' folder in the root path of the application.

    It means that the filename argument will take a relative path to your file in static_folder and convert it to a relative path combined with static_url_default:

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

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

    So if you want to get files from the static/bootstrap folder you use this code:

    
    

    Which will be converted to (using default settings):

    
    

    Also look at url_for documentation.

提交回复
热议问题