模板中使用for循环结构:
result.html的Template部分使用for循环将字典对象result{}的键和值对呈现为HTML表的单元格。
- result.html
<!doctype html>
<table border = 1>
{% for key, value in result.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
- template1.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
###"/"不加任何地址result
def result():
dict = {'phy':50,'che':60,'maths':70}
return render_template('result.html', result = dict)
if __name__ == '__main__':
app.run(debug = True)
- 网页结果:
这里不知道为什么地址后面加result会报错,但是不加就没问题了