default过滤器:如果当前变量不存在,这时候可以指定默认值,
length过滤器:求列表或者字符串字典元组的长度
#encoding:utf-8 from flask import Flask,render_template app = Flask(__name__) @app.route('/') def index(): comments=[{ 'user':u'二狗', 'content':'jierjioeriorei' }, { 'user':u'大狗', 'content':'vjoirjoirjior' } ] return render_template('index.html',commnets=comments) if __name__ == '__main__': app.run(debug=True) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <img src="{{ avatar|default('https://img-ph-mirror.nosdn.127.net/NN7OOz8ODXd47U867WzzuA==/3171660037675940136.png?imageView&thumbnail=223y124&quality=100&crop=0_0_223_94')}}"> <hr> <p>评论数({{ commnets|length}})</p> <ul> {% for commnet in commnets %} <li> <a href="https://img-ph-mirror.nosdn.127.net/NN7OOz8ODXd47U867WzzuA==/3171660037675940136.png?imageView&thumbnail=223y124&quality=100&crop=0_0_223_94">{{commnet.user}}</a> <p>{{commnet.content}}</p> </li> {% endfor %} </ul> </body> </html>