模板传参

from flask import Flask,render_template


app = Flask(__name__)


@app.route('/')
def hello_world():

    context = {
        'username' : "zhiliao",
        'age': 18,
        'country' : "china",
        'childrens' : {
            'name':"abc",
            "height":180,
        }
    }

    return render_template('index.html',**context)#context=context这种写法太low了
  #如果这里不明白可以看下源码

if __name__ == '__main__':
    app.run(debug=True)

前端模块渲染

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>标题</title>
</head>
<body>

    <h1>文章标题</h1>
    <h2>{{ username }}</h2>
    <p>{{ age }}</p>
    <p>{{ country }}</p>
    <p>{{ childrens.name }}</p>
    <p>{{ childrens['height'] }}</p>#这里根据字典键值对取值
</body>
</html>
原文地址:https://www.cnblogs.com/wuheng-123/p/9671591.html