使用Flask fram时出现404错误

2024-04-26 14:42:56 发布

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

我试图使用flask框架运行我的html代码。当我试图运行python脚本时,它在浏览器中显示了404 error

<html>
   <body>

      <h1>Hello world!</h1>

   </body>
</html>

python脚本

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

@app.route('/hello/<user>')
def hello_name(user):
   return render_template('hello.html', name = user)

if __name__ == '__main__':
   app.run(debug = True)

这个错误的原因是什么


Tags: 代码name脚本框架appflaskhellohtml
2条回答

用jinja打印你的名字

<html>
   <body>
      <h1>Hello {{name}}</h1>
   </body>
</html>

穿过flask quickstart

代码运行良好。 确保将html文件保存在templates文件夹中。我的文件夹结构如下:

flask_app
├── hello.py
└── templates
    └── hello.html

而且访问http://127.0.0.1:5000/也会导致错误,因为您没有为根路径定义任何视图

在访问索引路径http://127.0.0.1:5000/hello/arsho时,我得到了预期的视图:

Hello world in flask

相关问题 更多 >