【问题标题】:Jinja2Exception Flask template not found找不到 Jinja2Exception Flask 模板
【发布时间】:2021-05-26 08:31:41
【问题描述】:

我收到错误 jinja2.exceptions.TemplateNotFound。即使我的模板文件夹中有我的 HTML 文件,我仍然收到此错误。前段时间运行良好,现在出现此错误。

完整的错误代码:

Traceback (most recent call last)
File "E:\price\web\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "E:\price\web\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "E:\price\web\Lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "E:\price\web\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\price\web\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "E:\price\web\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "E:\price\web\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "E:\price\web\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "E:\price\web\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "E:\price\web\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "E:\price\web\flasktest.py", line 6, in index
return render_template('about.html')
File "E:\price\web\Lib\site-packages\flask\templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "E:\price\web\Lib\site-packages\jinja2\environment.py", line 1068, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "E:\price\web\Lib\site-packages\jinja2\environment.py", line 997, in get_template
return self._load_template(name, globals)
File "E:\price\web\Lib\site-packages\jinja2\environment.py", line 958, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "E:\price\web\Lib\site-packages\jinja2\loaders.py", line 125, in load
source, filename, uptodate = self.get_source(environment, name)
File "E:\price\web\Lib\site-packages\flask\templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "E:\price\web\Lib\site-packages\flask\templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: about.html

这是我的代码 sn-p:

from flask import Flask, render_template

app = Flask('test', template_folder= 'templates')
@app.route('/')
def index():
    return render_template('about.html')

if __name__ == '__main__':
    app.run(debug = True, host = '127.0.0.1', port = 5000)

这是我的目录的屏幕截图:

更新:

  1. 我的代码在新文件中运行良好。是缓存的原因吗?

  2. 尝试过 app = Flask(name) 但仍然是同样的问题。

  1. 新的 Test.py 文件完全可以正常工作,但旧的就不行了。

【问题讨论】:

  • 你试过render_template('templates/about.html')吗?
  • 试过但没有运气:(

标签: python python-3.x flask


【解决方案1】:

看看这个主题https://flask.palletsprojects.com/en/2.0.x/api/#application-object 并阅读本节关于第一个参数

所以我猜,你应该导入正确的包名,

所以试试__name__ 而不是test

app = Flask(__name__, template_folder='templates')

顺便说一句,Flask 已经期望 templates 作为默认文件夹来查看,这意味着它是可选的,您可以像这样定义您的应用程序

app = Flask(__name__)

【讨论】:

  • 感谢您的解决方案,但我已经尝试过了,我更新了上面的代码截图。你可以检查一次,它不起作用。
  • 谢谢你,我没有注意到我在做 'name' 而不是 name
【解决方案2】:

Flask 默认在“模板”文件夹https://flask.palletsprojects.com/en/2.0.x/quickstart/#rendering-templates 中查找模板。

试试这个:

app = Flask(__name__)
  @app.route('/')
  def index():
    return render_template('about.html')

【讨论】:

  • 感谢您的解决方案。但我试过了,但没有成功,所以我不得不给出位置。但是当我在 py 新文件中尝试相同的代码时,Flask 正在工作,但在旧文件中却没有。
  • 您是否设置了 FLASK_APP 环境变量以指向正确的文件?
  • 是的,比如 set FLASK_APP = flasktest.py 对吗?它显示 500 内部服务器错误。
【解决方案3】:

我终于找到了出错的解决方案。

我正在这样做:

app = Flask('__name__')

但正确的做法是这样的:

app = Flask(__name__)

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2017-08-13
    • 2021-04-05
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多