Flask blueprint static directory does not work?

后端 未结 5 1155
攒了一身酷
攒了一身酷 2020-12-08 09:56

According to the Flask readme, blueprint static files are accessible at blueprintname/static. But for some reason, it doesn\'t work.

My blueprint is lik

5条回答
  •  悲哀的现实
    2020-12-08 10:47

    I'm a little late, but none of the previous answers did really help me in achieving what I needed. I found out that I needed to do a bit of "tricks" to make the module's static folder work as I intended. My application is composed of several modules, but for now just pretend we had "application" as the main application folder and "locations" as the module's folder where a local "static" directory is placed (where we need to load static files for this module). The following is what I did:

    1. In the main application's __init__py file I added url_prefix='/' to my Blueprint's definition
    2. In my module's routes.py file I used both static parameters: static_folder='static' and 'static_url_path='/locations/static' to declare the Blueprint
    3. In the html template (which extends a template in the main application's folder), I used the following to retrieve the path to the static css file: {{url_for('locations_bp.static', filename='css/style.css')}} (locations_bp is my Bluprint's name for "locations" module)

    This way, the css file is loaded as /locations/static/css/style.css and this is exactly what I wanted to achieve: above all, the url_prefix I used allows me to avoid having a prefix on all my module's routes

提交回复
热议问题