from flask import Flask,render_template app = Flask(__name__) app.config.update(TEMPLATES_AUTO_RELOAD=True,Debug=True) @app.route('/') def hello_world(): context={ 'books':[ { 'name':'三国演义', 'author':'罗贯中', 'price':110 },{ 'name':'西游记', 'author':'吴承恩', 'price':109 },{ 'name':'红楼梦', 'author':'曹雪芹', 'price':120 },{ 'name':'水浒传', 'author':'施耐庵', 'price':119 } ] } return render_template('index.html',**context) if __name__ == '__main__': app.run(debug=True,port=8000)
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>zihliaokecheng</title> </head> <body> <table> <thead> <tr> <th>序号</th> <th>书名</th> <th>作者</th> <th>价格</th> </tr> </thead> <tbody> {% for book in books %} {% if loop.first %} <tr style="background:red "> {% elif loop.last %} <tr style="background: red"> {% else %} <tr> {% endif %} <td>{{ loop.index0 }}</td> <td>{{ book.name }}</td> <td>{{ book.author }}</td> <td>{{ book.price }}</td> </tr> {% endfor %} </tbody> </table> </body> </html>
