flask---模板访问属性和字典

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GoodLuckAC/article/details/79083050

在模板中,如果使用一个变量,语法是‘{{params}}’

访问模型中的属性或者是字典可以通过‘{{params.property}}’的形式。详见代码:
template02

#encoding:utf-8
from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')
def index():
    class Person(object):
        name=u'二狗'
        age=18
    p=Person()

    context={
       'username':u'大狗',
       'sex':'male',
       'age':18,
       'Person':p,
       'websites':
    {
      'baidu':'wwww.baidu.com',
      'google':'wwww.google.com'
    }
    }
    return render_template('index.html',**context)


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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hehehehehehehehehe</title>
</head>
<body>
<p>用户名:{{username}}</p>
<p>性别:{{sex}}</p>
<p>年龄:{{age}}</p>
<hr>
 <meta charset="UTF-8">
<p>名字:{{Person.name}}</p>
<p>年龄:{{Person.age}}</p>
<hr>

  <p>  百度:{{websites.baidu}}</p>
   <p> 谷歌:{{ websites.google}}</p>

</body>
</html>





猜你喜欢

转载自blog.csdn.net/GoodLuckAC/article/details/79083050
今日推荐