7

我正在学习 Flask Web 开发,我正在学习的教程介绍了一个名为Flask-Bootstrap的扩展。要使用此扩展,您必须先对其进行初始化,如下所示:

from flask_bootstrap import Bootstrap
# ...
bootstrap = Bootstrap(app)

对我来说很奇怪的bootstrap是,我的模块的其余部分没有使用该变量。但是,如果我注释掉这一行,jinja2.exceptions.TemplateNotFound就会引发异常。此外,使用的模板以这一行开头:

{% extends "bootstrap/base.html" %}

但我没有一个名为的/bootstrap目录/templates

我想知道发生了什么:

  1. 线路有什么作用bootstrap = Bootstrap(app)
  2. bootstrap/base.html居住在哪里?
4

2 回答 2

4
  • 其他答案已经告诉你

bootstrap = Bootstrap(app) 行“初始化应用程序上的扩展”。

bootstrap/base.html 位于 Flask-Bootstrap 包中。

要理解这一点,你必须对“Flask 的模板搜索路径”有所了解

  1. 应用程序的模板文件夹
  2. 蓝图的模板文件夹

所以 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 -
于 2018-07-12T08:17:37.887 回答
2

正如@davidism 在他的评论中所说,bootstrap = Bootstrap(app)“在应用程序上安装扩展”这一行。这种安装背后的机制超出了这个答案的范围。

位于bootstrap/base.htmlFlask-Bootstrap 包中。例如,在我的机器上,它的绝对路径是:

/Users/sunqingyao/Documents/Projects/flasky/venv/lib/python2.7/site-packages/flask_bootstrap/templates/bootstrap/base.html

这是它的内容:

{% block doc -%}
<!DOCTYPE html>
<html{% block html_attribs %}{% endblock html_attribs %}>
{%- block html %}
  <head>
    {%- block head %}
    <title>{% block title %}{{title|default}}{% endblock title %}</title>

    {%- block metas %}
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {%- endblock metas %}

    {%- block styles %}
    <!-- Bootstrap -->
    <link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="stylesheet">
    {%- endblock styles %}
    {%- endblock head %}
  </head>
  <body{% block body_attribs %}{% endblock body_attribs %}>
    {% block body -%}
    {% block navbar %}
    {%- endblock navbar %}
    {% block content -%}
    {%- endblock content %}

    {% block scripts %}
    <script src="{{bootstrap_find_resource('jquery.js', cdn='jquery')}}"></script>
    <script src="{{bootstrap_find_resource('js/bootstrap.js', cdn='bootstrap')}}"></script>
    {%- endblock scripts %}
    {%- endblock body %}
  </body>
{%- endblock html %}
</html>
{% endblock doc -%}

base.html只要我在文档中找到相关部分,就会添加“ Jinja2 如何找到”的答案。

于 2016-09-10T15:36:52.847 回答