【问题标题】:Blueprint template folder in flask not working as expected烧瓶中的蓝图模板文件夹未按预期工作
【发布时间】:2018-04-17 21:53:35
【问题描述】:

这是我的项目结构:

├── 运行.py └── test_blueprint ├── __init__.py ├── __init__.pyc ├── 主站 │   ├── 控制器 │   │   ├── controllers.py │   │   ├── controllers.pyc │   │   ├── __init__.py │   │   └── __init__.pyc │   ├── __init__.py │   ├── __init__.pyc │   ├── 静态 │   │   ├── css │   │   │   ├── bootstrap.min.css │   │   │   └── signin.css │   │   └── js │   └── 模板 │   └── 主站 │   └── homepage.html ├── 静态 │   ├── css │   │   ├── bootstrap.min.css │   │   └── signin.css │   └── js ├── 模板 │   └── 主站 │   └── index1.html └── 用户管理 ├── 控制器 │   ├── controllers.py │   ├── controllers.pyc │   ├── __init__.py │   └── __init__.pyc ├── __init__.py ├── __init__.pyc ├── 静态 └── 模板

这里我有两个蓝图,主站点和用户管理。我已将它们注册在 test_blueprint 文件夹下的文件 __init__.py 中,该文件夹是主文件夹(blueprint_project 文件夹下的一个具有 run.py 的文件夹)。

__init__ test_blueprint 下的文件

from flask import Flask
app = Flask(__name__)
from test_blueprint.mainsite.controllers.controllers import mod 
from test_blueprint.usermanagement.controllers.controllers import mod 

app.register_blueprint(mainsite.controllers.controllers.mod, url_prefix="/")
app.register_blueprint(usermanagement.controllers.controllers.mod, url_prefix="/usermanagement")

现在发布我创建的每个蓝图文件夹下的 __init__.py,我在其中定义了蓝图和模板文件夹。但是,它不会从那里获取模板。它不断抛出错误“找不到模板”。所以,我在 test_blueprint 文件夹下创建了模板文件夹。它完美地从模板/主站点文件夹中选择 index.html。
另外,如果我提供 /home/user/flaskenv/blueprint_project/... templates 文件夹,这是我的模板在 blueprint 文件夹下的绝对路径,它可以正常工作。

不确定该错误是否存在于 Flask 中。我在 YouTube 上看到了一段视频,并希望我的烧瓶项目表现相同。可惜没有。

这是我的 controller.py/views.py 文件在主站点中的样子:

from flask import Blueprint, render_template, url_for

mod = Blueprint('mainsite', __name__, static_folder='static',template_folder='templates')

@mod.route("/")
def home():
    return render_template('mainsite/homepage.html')

现在如果我使用os.path.abspath 来定位蓝图文件夹下的模板文件夹,然后将该路径作为变量传递给template_folder,它就可以正常工作了。

现在我根据烧瓶文档的期望是,仅提及 template_folder='templates' 应该会自动在蓝图文件夹下找到模板,否则它完全违背了目的。

【问题讨论】:

    标签: templates flask


    【解决方案1】:

    我通过用唯一名称重命名 html 文件解决了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多