由于Django的模板引擎和Flask中的Jinja2模板引擎有很多一样的地方,所以我将一样的地方总结到了独立的文章中
https://www.cnblogs.com/kuxingseng95/articles/9091027.html
构造上下文
构造上下文前
return render_template('test01_template.html', my_list=my_list, my_int=my_int, my_dict=my_dict)
构造上下文后
return render_template('test01_template.html', context=context)
return render_template('test01_template.html', **context)
比如:
from flask import Flask, render_template app = Flask(__name__) @app.route("/index") def index(): # 处理业务逻辑 data = { "name": "python", "age": 18, "my_li": [1, 2, 3, 4, 5, 6, 7], "my_int": 0, "my_dict": { "city": "bj" } } return render_template("code_18_templates.html", **data) if __name__ == '__main__': app.run(debug=True)