flask中模板引入
flask中模板采用的是jinja,这是一个非常小巧且便捷的模板引擎。实用方法如下:
- 引入模块 from flask import render_template
- 创建模板文件夹 templates
- 调用render_template("index.html",data=data)进行模板条用和赋值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>demo index</title>
</head>
<body>
<h1>index page </h1>
<ul>
{% for d in data %}
<li>{{ d.name }}今年{{ d.age }}岁了</li>
{% endfor %}
</ul>
</body>
</html>
from flask import Flask,url_for,render_template
app = Flask(__name__)
@app.route("/")
def index():
data = [{"name":"menghuiguli","age":"28"},{"name":"lisi","age":"22"},{"name":"wangwu","age":"40"},{"name":"zhaoliu","age":"18"}]
return render_template("index.html",data=data)
1人点赞