即使模板文件存在,Flask 也会引发 TemplateNotFound 错误 [英] Flask raises TemplateNotFound error even though template file exists

查看:28
本文介绍了即使模板文件存在,Flask 也会引发 TemplateNotFound 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试呈现文件 home.html.该文件存在于我的项目中,但是当我尝试渲染它时,我一直收到 jinja2.exceptions.TemplateNotFound: home.html.为什么 Flask 找不到我的模板?

I am trying to render the file home.html. The file exists in my project, but I keep getting jinja2.exceptions.TemplateNotFound: home.html when I try to render it. Why can't Flask find my template?

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')

/myproject
    app.py
    home.html

推荐答案

您必须在正确的位置创建模板文件;在 python 模块旁边的 templates 子目录中(== 您创建 Flask 应用程序的模块).

You must create your template files in the correct location; in the templates subdirectory next to the python module (== the module where you create your Flask app).

该错误表明templates/ 目录中没有home.html 文件.确保您在与 python 模块相同的目录中创建了该目录,并且您确实在该子目录中放置了一个 home.html 文件.如果您的应用是一个包,则应在包内创建模板文件夹.

The error indicates that there is no home.html file in the templates/ directory. Make sure you created that directory in the same directory as your python module, and that you did in fact put a home.html file in that subdirectory. If your app is a package, the templates folder should be created inside the package.

myproject/
    app.py
    templates/
        home.html

myproject/
    mypackage/
        __init__.py
        templates/
            home.html

或者,如果您将模板文件夹命名为 templates 以外的其他名称,并且不想将其重命名为默认值,您可以告诉 Flask 使用该其他目录.

Alternatively, if you named your templates folder something other than templates and don't want to rename it to the default, you can tell Flask to use that other directory.

app = Flask(__name__, template_folder='template')  # still relative to module

您可以通过设置 ,让 Flask 解释它如何尝试找到给定模板EXPLAIN_TEMPLATE_LOADING 选项True.对于加载的每个模板,您都会得到一个记录到 Flask app.logger,在 INFO 级别.

You can ask Flask to explain how it tried to find a given template, by setting the EXPLAIN_TEMPLATE_LOADING option to True. For every template loaded, you'll get a report logged to the Flask app.logger, at level INFO.

这是搜索成功时的样子;在这个例子中,foo/bar.html 模板扩展了 base.html 模板,所以有两个搜索:

This is what it looks like when a search is successful; in this example the foo/bar.html template extends the base.html template, so there are two searches:

[2019-06-15 16:03:39,197] INFO in debughelpers: Locating template "foo/bar.html":
    1: trying loader of application "flaskpackagename"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /.../project/flaskpackagename/templates
       -> found ('/.../project/flaskpackagename/templates/foo/bar.html')
[2019-06-15 16:03:39,203] INFO in debughelpers: Locating template "base.html":
    1: trying loader of application "flaskpackagename"
       class: jinja2.loaders.FileSystemLoader
       encoding: 'utf-8'
       followlinks: False
       searchpath:
         - /.../project/flaskpackagename/templates
       -> found ('/.../project/flaskpackagename/templates/base.html')

蓝图也可以注册自己的模板目录,但这不是必需的,如果您正在使用蓝图来更轻松地跨逻辑单元拆分更大的项目.即使在每个蓝图使用其他路径时,也始终首先搜索 Flask 应用模板主目录.

Blueprints can register their own template directories too, but this is not a requirement if you are using blueprints to make it easier to split a larger project across logical units. The main Flask app template directory is always searched first even when using additional paths per blueprint.

这篇关于即使模板文件存在,Flask 也会引发 TemplateNotFound 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆