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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM