Python Flask 返回html文件

1、在templates文件夹建立一个html文件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<h2>This is index page</h2>
</body>
</html>

 

2、从flask中导入render_template,整体代码如下:

from flask import Flask, render_template
import config
 
app = Flask(__name__)
app.config.from_object(config)
 
 
@app.route('/')
def index():
    return render_template('index.html')
 
if __name__ == '__main__':
    app.run()

render_template函数会自动在templates文件夹中找到对应的html

posted @ 2022-10-15 01:16  青山原  阅读(1454)  评论(0编辑  收藏  举报