代码之家  ›  专栏  ›  技术社区  ›  Tri

如何循环烧瓶Jinja2表格输入。。?

  •  0
  • Tri  · 技术社区  · 8 年前

    我有一个程序,可以用来订票,如果用户订票三张它的值存储到变量 ,我想根据该变量的值进行循环表单输入 预订
    像这个表单一样,如果我预订了三个ticker,表单也会显示三个输入表单。

    enter image description here
    应用程序.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>
    

    所以。。怎么做。。?

    1 回复  |  直到 8 年前
        1
  •  2
  •   gittert    8 年前

    我测试了一下,这应该行得通。将返回渲染模板重写为:

    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>