微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

即使模板文件存在,使用Flask时也会引发TemplateNotFound错误500; jinja2.exceptions.TemplateNotFound

如何解决即使模板文件存在,使用Flask时也会引发TemplateNotFound错误500; jinja2.exceptions.TemplateNotFound

我想实现一个机器学习模型,但是我实现了flask,即使我有template文件夹,我也找不到此错误模板。 我已经尝试了所有关于堆栈溢出的可能解决方案,但是没有任何效果,请帮助我。 基本代码是运行索引页,甚至无法正常工作。

from flask import Flask,render_template
app = Flask(__name__,template_folder="C:/Users/sweet/Music/Rumor_Detection_Project_copy/Front_End/templates")
app.debug=False
@app.route('/')

def index():
    with app.app_context(),app.test_request_context():
        template = render_template('C:/Users/sweet/Music/Rumor_Detection_Project_copy/Front_End/templates/index.html')



if __name__ == "__main__":
    app.run()

我尝试过的是:

请帮助,因为这是基本知识,我还必须做很多其他事情

code with error description

解决方法

模板应与flask脚本存储在同一目录中,然后可以通过将html文件的名称传递给render_template()函数来使用它。

此外,装饰器(@app.route('/'))和def函数之间不应有空白行。

您的代码应如下所示:

@app.route('/')
def index():
    with app.app_context(),app.test_request_context():
        template = render_template('index.html')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。