Flask--过滤器default与length

######################################################
from flask import Flask,render_template
app = Flask(__name__)


@app.route('/')
def index():
pic = 'http://avatar.csdn.net/9/B/4/3_wspba.jpg'
return render_template('index5.html')


if __name__ == '__main__':
app.run(debug=True)
————————————————————————————————————————————————————————
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>过滤器</title>
</head>
<body>
<img src="{{ pic|default('http://img04.sogoucdn.com/app/a/100520024/c0b421e93dbfd59a7f07139f511efab4')}}" alt=" ">
{#default过滤器 示例满足的需求:如果没有传头像,则使用默认头像#}

</body>
</html>
####################################################################################
from flask import Flask,render_template
app = Flask(__name__)


@app.route('/')
def index():
comments=[
{
'user':'abc',
'content':'abc'
},
{
'user': 'abcd',
'content': 'abcdabcdabcd'
},
{
'user': 'abcde',
'content': 'abcdeabcdeabcdeabcdeabcde'
}

]
return render_template('index5.html',comments=comments)


if __name__ == '__main__':
app.run(debug=True)
----------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>过滤器</title>
</head>
<body>
<img src="{{ pic|default('http://img04.sogoucdn.com/app/a/100520024/c0b421e93dbfd59a7f07139f511efab4')}}" alt=" ">
{#default过滤器 示例满足的需求:如果没有传头像,则使用默认头像#}
{#length过滤器# {{ comments|length }} 计算comments的长度}
<hr>
<p>评论数:({{ comments|length }})</p>

</body>
</html>


posted @ 2017-12-27 17:31  王亚锋  阅读(912)  评论(0编辑  收藏  举报