python - Flask-Bootstrap 是如何工作的?

标签 python twitter-bootstrap templates flask flask-bootstrap

我正在学习 Flask 网络开发,我正在学习的教程介绍了一个名为 Flask-Bootstrap 的扩展。要使用这个扩展,你必须先初始化它,就像这样:

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

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

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

但是我在/templates下没有名为/bootstrap的目录!

我想知道发生了什么:

  1. bootstrap = Bootstrap(app) 行是做什么的?
  2. bootstrap/base.html 在哪里?

最佳答案

  • 其他答案已经告诉你了

the bootstrap = Bootstrap(app) line "init the extension on the app".

The bootstrap/base.html resides in the Flask-Bootstrap package.

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

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

所以 Flask-Bootstrap 实际上是为你的应用注册了一个蓝图:

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 -

关于python - Flask-Bootstrap 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427256/

相关文章:

python - Pandas GroupBy 的每一列都有不同的石斑鱼

python - 什么时候全局安装外部Python包,什么时候本地安装? pip 还是系统包管理器?

C++ ostream 使用模板隐式删除

C++ 概念看到我的类型的函数,但看不到 std::vector 的函数

python - 将日期时间对象转换为字符串 Python

python - 将对象与对象方法一起传递给函数

css - IE10/11 中的 Flexbox 网格未对齐

css - 如何让两列垂直对齐?

javascript - 将隐藏元素添加到出现在滚动条上的粘性菜单

c++ - 在另一个命名空间中定义一个符号