使用render_template能够传递载参数进行模板渲染
这里直接贴一下个人测试代码html
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def test(): context = { 'title_name':"test_render", 'name':"sp4rk", 'test_variable':"It works" } return render_template('a.html', **context) if __name__ == '__main__': app.run(debug = True)
<!--a.html--> <html> <title>{{title_name}}</title> {% if name %} <h1>{{name}}:test name</h1> {% else %} <h1>Hello word!</h1> {% endif %} <h1>{{test_variable}}:test variable</h1> </html>
python 中的**context 型参中传值以字典的方式呈现,能够参照python函数——形参中的:*arg和**kwargs
python