Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple static folders & templates folders in flask app

Is it possible to have more than one static folder in flask app? I have some .css and .js files which I am using only for certain blueprint and would like to have the files in same directory as blueprint.

My app has structure like this:

app
|
| blueprints
| |
| | blueprint_1
| | |
| | | views.py
| | | __init__.py
| | 
| | blueprint_2
| | |
| | | views.py
| | | __init__.py
| 
| static
| |
| | css, js, etc.

And I would like to have structure like this:

app
|
| blueprints
| |
| | blueprint_1
| | |
| | | views.py
| | | __init__.py
| | | static
| | | |
| | | | certain css, js, etc
| | 
| | blueprint_2
| | |
| | | views.py
| | | __init__.py
| | | static
| | | |
| | | | certain css, js, etc
| 
| static
| |
| | css, js, etc.

And similar thing with templates. While I am using .js file in template I access it with:

{{url_for('static'), filename='jsfile'}}

like image 413
w8eight Avatar asked Oct 15 '25 08:10

w8eight


1 Answers

Okay, I figured it out.

According to docs, if the blueprint does not have a url_prefix, it is not possible to access the blueprint’s static folder.

The solution then is:

Add url_prefix to Blueprint:

some_blueprint = Blueprint('something', __name__, static_folder='static', url_prefix='/something')

and in the template we refer to our static folder as it follows:

{{url_for('something.static', filename=...)}}

It seems kinda weird to me, that url_prefix is necessary for it to work, but whatever.

like image 96
w8eight Avatar answered Oct 17 '25 00:10

w8eight



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!