Flask入门2

Flask渲染模板和传参

1.如何渲染模板
*模板放在templates文件夹下
*从flask里面引入render_template函数

from flask import render_template

*在视图函数中,使用render_template
2.模板传参
*如果只有一个参数或者少量参数,直接在render_template中函数中添加关键字参数既可
*有多个参数的时候,可以先把参数放在字典中,然后在render_template中使用两个星号,把字典转换为关键参数传回去,如下

@app.route('/')
def index():
    context={
        'username':'韩文远',
        'gender':'男'
    }
    return render_template('index.html',**context)

3.在模板中,如何使用一个变量语法是{params}}
4.使用模型中的属性或者是字典 ,可以通过{{params.property}}或者{{params[‘age’]}}

你可能感兴趣的:(flask)