Search code examples
static-filesflask

flask blueprint for custom static folder not working in templates using url_for


I am trying to use a blueprint to serve some of my js and css files from a folder than /static. I want these files to be available from the root. I have registered the following blueprint with the app:

custom = Blueprint('/', __name__, static_folder='custom', static_url_path='/custom')
app.register_blueprint(custom)

I can find the desired files in the expected place, ie. /custom/custom.css by putting this url directly into the browser, however, I am not able to access them in templates using url_for like this:

<link href="{{ url_for('custom', filename='style.css') }}" rel="stylesheet">

I get: BuildError: ('custom', {'filename': 'style.css'}, None)

I have tried any number of combinations ie.:

custom = Blueprint('custom', __name__, static_folder='custom', static_url_path='/custom')

with

<link href="{{ url_for('custom.custom', filename='style.css') }}" rel="stylesheet">

etc but nothing is working for me.


Solution

  • You need to use the blueprint's static endpoint.

    url_for('custom.static', filename='custom.css')