我有一个程序,可以用来订票,如果用户订票三张它的值存储到变量 ,我想根据该变量的值进行循环表单输入 预订 像这个表单一样,如果我预订了三个ticker,表单也会显示三个输入表单。
应用程序.py
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def index(): booking = 3 return render_template('index.html') if __name__ == "__main__": app.run(host='0.0.0.0', debug=True)
index.html索引
<body> <form action=""> {% for contact in booking %} Name: <br> <input type="text" value="{{ contact }}"><br> Age: <br> <input type="text" value="{{ contact }}"> {% endfor %} </form> </body>
所以。。怎么做。。?
我测试了一下,这应该行得通。将返回渲染模板重写为:
return render_template('index.html', booking = booking)
并按如下方式重写html:
<body> <form action=""> {% for i in range(booking) %} Name: <br> <input type="text" value="{{ contact }}"><br> Age: <br> <input type="text" value="{{ contact }}"> {% endfor%} </form> </body>