为什么Flask不使用下面的代码呈现html页面?

2024-05-23 14:03:39 发布

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

我在AWS C9平台上启动了一个新项目,在用frameworkflask呈现网页时遇到了一个问题

框架运行正常,它可以返回消息(默认情况下),但当我尝试返回模板(Html页面)时,我收到错误消息:“TemplateNotFound” 我会留下下面的代码和错误信息

import os
from flask import Flask, render_template, request
app = Flask(__name__)



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

@app.route("/about")
def about():
    return "<h1>Flask is working</h1>"    


if __name__=="__main__":
    app.run(host='0.0.0.0', port=8080)
    app.debug = True

堆栈跟踪:-在

^{pr2}$

Tags: nameimportawsapp消息flaskhomereturn
1条回答
网友
1楼 · 发布于 2024-05-23 14:03:39

默认情况下,flask在templates文件夹中查找html模板。不需要在return语句中显式地追加/templates。在

用这个

return render_template('home.html')

相关问题 更多 >