码迷,mamicode.com
首页 > 其他好文 > 详细

9_flask中的模板渲染和模板传参及其技巧

时间:2019-10-07 19:36:25      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:用两个   一个   使用   关键字   flask   后台   年龄   python   pre   

模板传参

在使用render_template 渲染模板的时候,可以传递关键字参数, 如果你的参数过多,那么可以将所有的参数放到一个字典中,然后 传这个字典参数的时,使用两个星号,将字典打散成关键字参数

  1. 后台传参
@app.route('/')
def index():
    # return render_template('index.html', name='long', age=18, country='china')

    context = {
        'name': 'long',
        'age': 18,
        'country': 'china'
    }
    return render_template('index.html', **context)
  1. 前台获取参数
<body>
    index
    <p>名字: {{ name }}</p>
    <p>年龄: {{ age }}</p>
    <p>国家: {{ country }}</p>
</body>

如果后台是下面这样子传

context = {
    'name': 'long',
    'age': 18,
    'country': 'china'
}
return render_template('index.html', context=context)

则前台应该这样子获取参数:

{{ context.name }}
{{ context.age }}
{{ context.country }}

9_flask中的模板渲染和模板传参及其技巧

标签:用两个   一个   使用   关键字   flask   后台   年龄   python   pre   

原文地址:https://www.cnblogs.com/nichengshishaonian/p/11631693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!