admin管理员组

文章数量:1392086

In Flask, I am attempting to nest blueprints but am running into issues with templates folders.

Prior to blueprints nesting, the fix was to duplicate the relative path to the blueprint in its templates folder.

app/bp/a/templates/a/index.html
app/bp/b/templates/b/index.html

This feels less acceptable with nested blueprints. For example, for blueprints:

app/bp/a/
app/bp/a/_id/
app/bp/a/_id/a1/
app/bp/a/_id/a1/_id/

app/bp/b/
app/bp/b/_id/
app/bp/b/_id/a1/
app/bp/b/_id/a1/_id/

Instead of (sorted by nest level, for readability):

app/bp/a/templates/index.html
app/bp/b/templates/index.html
app/bp/a/_id/templates/index.html
app/bp/b/_id/templates/index.html
app/bp/a/_id/a1/templates/index.html
app/bp/b/_id/a1/templates/index.html
app/bp/a/_id/a1/_id/templates/index.html
app/bp/b/_id/a1/_id/templates/index.html

I'd need (sorted by nest level, for readability):

app/bp/a/templates/a/index.html
app/bp/b/templates/b/index.html
app/bp/a/_id/templates/a/_id/index.html
app/bp/b/_id/templates/b/_id/index.html
app/bp/a/_id/a1/templates/a/_id/a1/index.html
app/bp/b/_id/a1/templates/b/_id/a1/index.html
app/bp/a/_id/a1/_id/templates/a/_id/a1/_id/index.html
app/bp/b/_id/a1/_id/templates/b/_id/a1/_id/index.html

At the deeper nested levels, this becomes overly redundant. It also requires manually entering the parent paths recursively into the template folder of every blueprint, making higher-level path name changes a nightmare.

How do I setup these blueprints to be able to:

  • Specify the blueprint (or its path) in addition to the template name in render_template().
  • Skip shadowing the parent path(s) in blueprint templates folders.

Should I just set templates_folder to absolutePathToAppRoot for every blueprint and then render_template() to <relativePathFromAppRoot>/templates/index.html?

本文标签: Flask Nested blueprints with templates with duplicate namesStack Overflow