这篇继续水 但是在水的的基础上 让搭建能正常使用flask 搭建一个站
上篇讲到在 模板view中 输入{{xx }} 可以打印 后台传的值。 这篇讲一下 循环控制 条件控制等
修改后台代码:
1 @app.route('/') 2 def hello_world(): 3 #return 'Hello World!' 4 #return jsonify({'key':'hello'}) #返回json 5 #return render_template_string('<html><head></head><body>1111:{{test}}</body></html>',test="tttt") #通过拼接字符串方式 6 return render_template('index.html', test="tttttt",alist=[1,2,3,4,5,6]) #普通返回 view 方式
修改前台代码:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 9 {{ test }} 10 11 {# 这是注释的写法 #} 12 {# 下面是 for 的写法 #} 13 {% for n in alist %} 14 {% if n >3 %} 15 {{ n }} 16 {% endif %} 17 {% endfor %} 18 19 20 </body> 21 </html>
注: for if 等表达式 需要用{% %} 注释 需要使用{# #}