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

2024-05-16 18:52:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想实现一个机器学习模型,但我实现了flask,我发现这个错误模板没有找到,即使我有模板文件夹。 我已经尝试了所有可能的解决方案给堆栈溢出,但没有工作,请帮助我。 基本代码是运行索引页,即使这样也不起作用

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()

我试过的是:

  • 将模板重命名为模板
  • 使用正确的子目录
  • 调试功能打开和关闭
  • 错误500的Catch语句

请帮忙,因为这是最基本的,我还需要做很多其他的事情

code with error description


Tags: nameproject模板appflask错误musictemplate
1条回答
网友
1楼 · 发布于 2024-05-16 18:52:11

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

另外,decorator(@app.route('/'))和def函数之间不应该有空行

您的代码应该如下所示:

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

相关问题 更多 >