bootstrap = Bootstrap(app) 行「アプリの拡張機能を初期化」。
bootstrap/base.html は Flask-Bootstrap パッケージにあります。
これを理解するには、「Flask のテンプレート検索パス」について知っておく必要があります。
- アプリケーションのテンプレート フォルダ
- ブループリントのテンプレート フォルダ
つまり、Flask-Bootstrap は実際に設計図をアプリに登録します。</p>
class Bootstrap(object):
def __init__(self, app=None):
if app is not None:
self.init_app(app)
def init_app(self, app):
blueprint = Blueprint(
'bootstrap',
__name__,
template_folder='templates',
static_folder='static',
static_url_path=app.static_url_path + '/bootstrap',
subdomain=app.config['BOOTSTRAP_LOCAL_SUBDOMAIN'])
app.register_blueprint(blueprint)
EXPLAIN_TEMPLATE_LOADING を設定すると、明確に確認できます。
app = Flask(__name__)
app.config['EXPLAIN_TEMPLATE_LOADING'] = True
それから
export FLASK_ENV=development
flask run
ページにアクセスすると:
[2018-07-12 15:28:58,659] INFO in debughelpers: Locating template "user.html":
1: trying loader of application "hello"
class: jinja2.loaders.FileSystemLoader
encoding: 'utf-8'
followlinks: False
searchpath:
- /root/learn/python-lab/Flask/flasky/templates
-> found ('/root/learn/python-lab/Flask/flasky/templates/user.html')
2: trying loader of blueprint "bootstrap" (flask_bootstrap)
class: jinja2.loaders.FileSystemLoader
encoding: 'utf-8'
followlinks: False
searchpath:
- /root/learn/python-lab/Flask/flasky/venv/lib/python3.6/site-packages/flask_bootstrap/templates
-> no match
################################################################# Note here #######
[2018-07-12 15:28:58,677] INFO in debughelpers: Locating template "bootstrap/base.html":
1: trying loader of application "hello"
class: jinja2.loaders.FileSystemLoader
encoding: 'utf-8'
followlinks: False
searchpath:
- /root/learn/python-lab/Flask/flasky/templates
-> no match ### in app path not found
2: trying loader of blueprint "bootstrap" (flask_bootstrap)
class: jinja2.loaders.FileSystemLoader
encoding: 'utf-8'
followlinks: False
searchpath:
- /root/learn/python-lab/Flask/flasky/venv/lib/python3.6/site-packages/flask_bootstrap/templates
## in blueprint path found the bootstrap/base.html
-> found ('/root/learn/python-lab/Flask/flasky/venv/lib/python3.6/site-packages/flask_bootstrap/templates/bootstrap/base.html')
127.0.0.1 - - [12/Jul/2018 15:28:58] "GET /user/Yao HTTP/1.1" 200 -