6、模板引擎简单使用
在根路径下创建 templates
文件夹,该文件夹用于放置模板文件
1、创建首页模板文件 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>这是首页</title>
</head>
<body>
<h1>这是首页!</h1>
</body>
</html>
2、创建用户模板文件 user.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>这是用户中心</title>
</head>
<body>
<h1>这是用户中心! </h1>
</body>
</html>
3、在根文件夹创建主程序文件
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html') # 跳转至模板文件
@app.route('/user/')
def user():
return render_template('user.html')
if __name__ == '__main__':
app.run(debug=True)