python - 如何循环 Flask Jinja2 表单输入..?
问题描述
我有一个可用于订票的程序,如果用户预订三张票,它的值存储到变量预订中,我想根据该变量预订的值进行循环表单输入。
像这个表格,如果我预订三个股票,表格也显示三个输入表格。
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)
索引.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>
你确定你想要相同的姓名和年龄值吗?
推荐阅读
- django - 带有下划线的Django模型名称?
- python - Jupyter 实验室/笔记本曾经可以工作,但现在打开时崩溃或无法打开
- search - 使用 Lucene 查找术语出现超过一定次数的文档
- javascript - 获取 Javascript 数组的唯一值
- c - 在 Linux 上检测键盘按键按下和释放
- python - 在 python 中为记录器设置格式化程序,而不是处理程序
- python-3.x - 使用 python 请求传递参数的问题
- asp.net-mvc - MVC 登录仅在 Firefox 中停止工作
- regex - 使用正则表达式确定字符串的所有后缀
- node.js - 我怎样才能在车把里各做一个?