python flask 模板渲染

render_template

使用render_template可以传递载参数进行模板渲染
这里直接贴一下我的测试代码

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

猜你喜欢

转载自www.cnblogs.com/spark-xl/p/9160339.html