flask blueprint template folder

As of Flask 0.8, blueprints add the specified template_folder to the app’s searchpath, rather than treating each of the directories as separate entities. This means that if you have two templates with the same filename, the first one found in the searchpath is the one used. This is admittedly confusing, and is poorly documented at this time (see this bug). It seems that you weren’t the only one that was confused by this behavior.

The design reason for this behavior is so that blueprint templates can be easily overriden from the main app’s templates, which are first-in-line in Flask’s template searchpath.

Two options come to mind.

  • Rename each of the index.html files to be unique (e.g. admin.html
    and main.html).
  • In each of the template folders, put each of the
    templates in a subdirectory of the blueprint folder and then call
    the template using that subdirectory. Your admin template, for example, would be yourapp/admin/pages/admin/index.html, and then called from within
    the blueprint as render_template('admin/index.html').

Leave a Comment

tech