代码之家  ›  专栏  ›  技术社区  ›  Charles R

烧瓶-嵌套字段列表未正确提交

  •  0
  • Charles R  · 技术社区  · 5 年前

    这是一个字段列表嵌套在其他字段列表中的表单。 目的是创建一个类似excel的表单。

    每个城市有一行,每个日期有一列(城市和日期是动态列表)。

    我将日期附加到城市,然后将城市附加到主表单。

    表单正在按预期加载:

    enter image description here

    但在提交后,看起来是这样的:

    enter image description here

    如何在提交后预先结束第二个附录?

    这是我的py文件:

    from flask import Flask
    from flask import Flask, flash, redirect, render_template, request, url_for
    from flask_wtf import FlaskForm
    from wtforms import StringField, FieldList, FormField, SubmitField
    
    
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'key'
    
    
    class DateForm(FlaskForm):
        class Meta:
            csrf = False
        date = StringField('date')
    
    class CityForm(FlaskForm):
        class Meta:
            csrf = False
        city = StringField('city')
        dates = FieldList(FormField(DateForm))
    
    class MainForm(FlaskForm):
        cities = FieldList(FormField(CityForm))
        submit = SubmitField('Save')
    
    
    @app.route('/', methods=['GET','POST'])
    def home():
    
        cities = ['NY', 'LA']
        dates = ["2018", "2019"]
    
        form = MainForm()
    
        if form.validate_on_submit():
            flash("validated on submit !", "alert alert-danger")
            redirect(url_for('home'))
    
        for c in cities:
            city = CityForm()
            city.city = c
    
            for d in dates:
                date = DateForm()
                date.date = d
                city.dates.append_entry(date)
    
            form.cities.append_entry(city)
    
        return render_template('template.html', form = form )
    
    if __name__ == "__main__":
        app.run(host="127.0.0.1", port="5000" ,debug=True)
    

    这是我的HTML模板:

    <html>
        <head>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
        </head>
        <body>
          <div class="container-fluid">
    
            <div class="row">
              <div class="col-sm-6 offset-sm-3 p-0">
                <form method="POST"  action="{{ url_for('home')}}">
                  {{ form.hidden_tag() }}
                  <div class="form-group">
                    <table>
                      {% for c in form.cities %}
                        <tr>
                          <td>{{ c.city }}</td>
    
                          {% for d in c.dates %}
                          <td>{{ d.date.data }}</td>
                          {% endfor %}
                        </tr>
                      {% endfor %}
                    </table>
                  </div>
                  <div class="form-group">
                      {{ form.submit(class="btn btn-secondary") }}
                  </div>
                </form>
              </div>
            </div>
    
            <div class="row">
              <div class="col-sm-6 offset-sm-3 p-0">
                {% with messages = get_flashed_messages(with_categories=true) %}
                  {% if messages %}
                    {% for category, message in messages %}
                      <p class="{{ category }}">{{ message }}</p>
                    {% endfor %}
                  {% endif %}
                {% endwith %}
              </div>
            </div>
    
          </div>
        </body>
    </html>
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   djnz    5 年前

    根据注释-缺少返回:

    return redirect(url_for('home'))