複雜控制語句

yongheng999發表於2024-07-19

我們想要顯示序號,可以透過loop.index來展示

這裡我們想遍歷一個複雜的列表字典,可以透過dict.items(),獲取到字典的鍵和值

實現效果:

程式碼實現:

from flask import Flask,render_template



app = Flask(__name__)
student={
"name":"zhangsan ","age":17,"gender":"男",

}

student_list=[
{
"name":"zhangsan ","age":17,"gender":"男"},
{"name":"lisi ","age":25,"gender":"男"},
{"name":"wangwu ","age":69,"gender":"女"},


]
student_dict=[{

"aaa":{"name":"zhangsan ","age":17,"gender":"男"},
"bbb":{"name":"lisi ","age":25,"gender":"男"},
"ccc":{"name":"wangwu ","age":69,"gender":"女"}
}

]


@app.route('/test1')
def test1():
return render_template('1.html',**student)

@app.route('/test2')
def test2():
return render_template('2.html',stu_list=student_list)

@app.route('/test3')
def test3():
return render_template('3.html',stu_dict=student_dict)


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

相關文章