算法部分代码:
def GetList(iMax): intList=[] for i in range(1,iMax+1): if(i%7==0): intList.append(i) return intList
前端部分代码:
<!DOCTYPE html> <html> <head> <title>JinJa2 Demo</title> <link rel="stylesheet" href="{{url_for('static',filename='bootstrap4/css/bootstrap.css')}}" type="text/css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.2/font/bootstrap-icons.css"> </head> <body> <div class="container mt-2"> <h2>JinJa2 Table Demo</h2> {% set student=[ {"name":"张三","sex":"男","math":80}, {"name":"李四","sex":"男","math":99}, {"name":"王麻子","sex":"男","math":100}, {"name":"赵莎莎","sex":"女","math":85.45}, {"name":"王伟","sex":"男","math":52}, {"name":"赵燕","sex":"女","math":91}, {"name":"王红兵","sex":"男","math":80}, {"name":"姚猪猪","sex":"女","math":57.45} ] %} <table class="table table-striped table-hover"> <thead> <tr> <th scope="col">编号</th> <th scope="col">姓名</th> <th scope="col">性别</th> <th scope="col">数学</th> </tr> </thead> <tbody> {% for stu in student|sort(attribute="math",reverse=true) %} <tr> <td>{{ loop.index}}</td> <td><i class="bi bi-person"></i>{{ stu.name }}</td> <td> {% set genderIcon="bi-gender-male" %} {% if stu.sex!="男" %} {% set genderIcon="bi-gender-female" %} {% endif %} <i class="{{genderIcon}}"></i>{{stu.sex}} </td> {% if stu.math<60 %} <td class="text-danger"><i class="bi bi-bookmark-check"></i> {{stu.math}}</td> {% else %} <td>{{stu.math}}</td> {% endif %} </tr> {% endfor %} </tbody> </table> </div> </body> </html>
---------------------
完整升级版:
后台代码:
myform.py
from wtforms import Form,IntegerField,SubmitField from wtforms.validators import InputRequired,NumberRange class AddForm(Form): iFirst=IntegerField('First',validators=[InputRequired(),NumberRange(min=0,max=100)]) iSecond=IntegerField('Second',validators=[InputRequired(),NumberRange(min=0,max=100)]) submit=SubmitField("提交")
JinJa2List.py
from HtzdLib import myform from flask import Flask,render_template,request,flash app=Flask(__name__) app.secret_key="12345" @app.route('/',methods=["GET","POST"]) def Index(): student=[{'name':'张三','sex':'男','math':80}, {'name':'老四','sex':'男','math':90}, {'name':'王麻子','sex':'男','math':80.8}, {'name':'王莎莎','sex':'女','math':40}, {'name':'赵燕','sex':'女','math':90}, {'name':'王红兵','sex':'男','math':68}] student=sorted(student,key=lambda s:(s["sex"],s["math"]),reverse=True) if(request.method=="GET"): return render_template("JinJa2List.html",student=student) app.run(debug=True,port=3363)
JinJa2List.html
<!DOCTYPE html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>列表显示Demo</title> <link rel="stylesheet" href="./static/bootstrap4/css/bootstrap.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.2/font/bootstrap-icons.css"> </head> <body> <div class="container border mt-2"> <h2 class="h2 text-center">列表显示Demo</h2> <table class="table"> <thead> <tr> <th>序号</th><th>姓名</th><th>性别</th><th>数学</th> </tr> </thead> <tbody> {% for stu in student %} <tr> <td>{{loop.index}}</td> <td><i class="bi bi-person"></i>{{stu.name}}</td> {% if stu.sex!='男' %} <td><i class="bi-gender-female"></i>{{stu.sex}}</td> {% else %} <td><i class="bi-gender-male"></i>{{stu.sex}}</td> {% endif %} {% if stu.math<60 %} <td class="bg-danger">{{stu.math}}</td> {% else %} <td><i class="bi bi-bookmark-check"></i>{{stu.math}}</td> {% endif %} </tr> {% endfor %} </tbody> </table> </div> </body> </html>