Flask014_ for 循环语句
- 调用
1 @app.route('/for') 2 def for_statement(): 3 books = [{ 4 'title': '三国演义', 5 'author': '罗贯中', 6 'price': 100 7 }, 8 { 9 'title': '水浒传', 10 'author': '施耐庵', 11 'price': 99 12 }, 13 { 14 'title': '红楼梦', 15 'author': '曹雪芹', 16 'price': 101 17 }, 18 { 19 'title': '西游记', 20 'author': '吴承恩', 21 'price': 102 22 } 23 ] 24 25 return render_template('for.html', books=books)
- for.html
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>for 循环</title> 6 <style> 7 table { 8 border-collapse: collapse; 9 text-align: center; 10 } 11 12 th, td { 13 padding-left: 20px; 14 padding-right: 20px; 15 height: 30px; 16 } 17 18 table, th, td { 19 border: 1px solid black; 20 } 21 22 .odd { 23 background-color: aliceblue; 24 } 25 </style> 26 </head> 27 <body> 28 <table> 29 <thead> 30 <tr> 31 <th>序号</th> 32 <th>书名</th> 33 <th>作者</th> 34 <th>价格</th> 35 <th>是否变化</th> 36 </tr> 37 </thead> 38 <tbody> 39 {% for book in books %} 40 <tr class="{{ loop.cycle('odd','even') }}"> 41 <td>{{ loop.index }}</td> 42 <td>{{ book.title }}</td> 43 <td>{{ book.author }}</td> 44 <td>{{ book.price }}</td> 45 <td>{{ loop.changed(book.price) }}</td> 46 </tr> 47 {% else %} 48 <tr> 49 <td colspan="4" style="text-align: center">无数据</td> 50 </tr> 51 {% endfor %} 52 </tbody> 53 </table> 54 </body> 55 </html>
- 效果