Flask--for循环遍历

'''
for循环与Python里面的保持一致
'''
##############################Python######################################

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


@app.route('/')
def index():
user={
'name':'wyf',
'age': '18'
}
website=['baidu','google']
# for k,v in user.items():
# print(k,v)
return render_template('index3.html',user=user,website=website)


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

##############################index3.html##################################
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index3</title>
</head>
<body>
{% for k,v in user.items() %}
<p>{{ k }}:{{ v }}</p>
{% endfor %}
{% for website in website %}
<p>{{ website }}</p>
{% endfor %}
</body>
</html>
###########################################################################

 ###########################python4#############################################

@app.route("/")
def index():
books=[
{
"name":"西游记",
"author": "吴承恩",
"price": 200
},
{
"name": "红楼梦",
"author": "曹雪芹",
"price": 300
},
{
"name": "水浒传",
"author": "施耐庵",
"price": 400
},
{
"name": '三国演义',
"author": '罗贯中',
"price": 500
}
]
return render_template('index3.html', books=books)


if __name__ == '__main__':
app.run(debug=True)
################html4##############################
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index4</title>
</head>
<body>
<table>
<thead>
<th>书名</th>
<th>作者</th>
<th>价格</th>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ book.name }}</td>
<td>{{ book.author }}</td>
<td>{{ book.price }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>

 



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