如何循环Flask Jinja2表单输入..? [英] How to looping Flask Jinja2 form input..?

查看:73
本文介绍了如何循环Flask Jinja2表单输入..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以用于机票预订的程序,如果用户预订将其价值存储到变量 booking 的三张机票,我想根据该变量 booking的值进行循环表单输入.
像这样的表格,如果我预订三个股票,该表格也会显示三个输入表格.

I have a program that can use for ticket booking, if user booking three ticket it value stored to variable booking, i want to make looping form input based on value from that variable booking .
like this form, if i booking three ticker, the form also display three input form.


这是我的 app.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>

那么..该怎么做..?

so.. how to do that..?

推荐答案

我测试了以下内容,这应该可以工作.将返回的渲染模板重写为此:

I tested the following, this should work. Rewrite your return render template to this:

return render_template('index.html', booking = booking)

并按如下所示重写您的html:

And rewrite your html as follows:

<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>

您确定要为姓名和年龄设置相同的值吗?

are you sure you want the same value for name and age btw?

这篇关于如何循环Flask Jinja2表单输入..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆