Flask中引入了jinja2模板引擎,可以显示动态数据、数据过滤、语句控制、模板继承和引用等。
实战实例
app.py
from flask import Flask,render_template app = Flask(__name__) @app.route('/') def index(): LibraryName="National Library" visitor={ "name":"Jack", "storey":"Computer" } categoryName=["Python","Linux","Flask"] return render_template("index.html",LibraryName=LibraryName,visitor=visitor,categoryName=categoryName) if __name__ == '__main__': app.run(host='0.0.0.0',port=8000,debug=True)View Code
index.html
mkdir templates && cd templates
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> {# National Library #} <h1>{{ LibraryName }}</h1> <h2>Dear {{ visitor.name }},</h2> <h3>Wellcome to {{ LibraryName }} ,as for {{ visitor.storey }} Professional Scholars</h3> <h3>looking for : {% for category in categoryName %} {{category}}, {% endfor %}books </h3> </body> </html>View Code
结果:
标签:__,index,FLASK,visitor,app,LibraryName,Flask,Jinja2,模块 From: https://www.cnblogs.com/jackbond/p/18102269