https://flask.palletsprojects.com/en/2.2.x/blueprints/#templates
The template folder is added to the search path of templates but with a lower priority than the actual application’s template folder.
Blueprintのコンストラクタで指定したtemplateフォルダは、テンプレート群の探索パスに追加されますが、アプリケーションのテンプレートよりも低い優先度を持ちます。
逆にいうとアプリケーションのテンプレートフォルダはBlueprintのコンストラクタで指定したテンプレートフォルダより優先されます。
そもそもテンプレートの探索は動的であって、Blueprintとアプリケーションとで場所は分離されているけれども探索の名前空間は共通です。
The reason for the extra admin folder is to avoid getting our template overridden by a template named index.html in the actual application template folder.
とある通り、Blueprint ごとのテンプレートフォルダの中に(冗長に思えるかもしれませんが)個別のディレクトリを用意してテンプレートの名前を切り分けるのが常套手段です。
アプリケーションの方のテンプレートが探索されて、そちらで上書きされるのを防ぐためです。
plain
1├── app.py
2├── blueprint_a
3│ ├── bp_templates
4│ │ └── blueprint_a
5│ │ └── index.html
6│ └── views.py
7└── templates
8 └── common
9 └── form.html
と作って、
app.py
python
1from flask import Flask
2
3from blueprint_a import views as bp_a
4
5app = Flask(__name__)
6app.register_blueprint(bp_a.app)
7
8if __name__ == "__main__":
9 app.run(debug=True)
blueprint_a/views.py
python
1from flask import Blueprint, render_template
2
3# Blueprintのオブジェクトを生成する
4app = Blueprint('views', __name__, url_prefix='/bp', template_folder='bp_templates')
5
6
7@app.route('/')
8def home():
9 title = "タイトルです"
10 return render_template('blueprint_a/index.html', title=title)
blueprint_a/bp_templates/blueprint_a/index.html
html
1<!DOCTYPE html>
2<html lang="ja">
3 <head>
4 <meta charset="UTF-8">
5 <title>{{title}}</title>
6 </head>
7
8 <body>
9 {% include 'common/form.html' %}
10 </body>
11</html>
templates/common/form.html
として、実行して
shell
1% curl http://localhost:5000/bp/
2<!DOCTYPE html>
3<html lang="ja">
4 <head>
5 <meta charset="UTF-8">
6 <title>タイトルです</title>
7 </head>
8
9 <body>
10 インクルードされた部分
11 </body>
12</html>
何に困っているのか分からない状況ですがひとまず。