0
votes

I've tried to simplify a previous question I asked earlier . I have a FieldList Form:

class TrackForm(Form):
    name = TextField('name')
    start = StringField('Start Time')
    end = StringField('End Time')
class Merge(Form):
    item_description = FieldList(FormField(TrackForm), min_entries=2, max_entries=6)

I call the form in the test template and fill in the fields for multiple entries before submitting.

<form method="POST" >
  {{ form.hidden_tag() }}

{{ form.item_description }}

<input class="btn btn-primary" style="margin-left:300px;"type="submit" value="Submit" />
</form>

I'm using the below to iterate over the entries and to hopefully store all of them in the variable 'newdata'.

@app.route('/test', methods=['GET', 'POST'])
def test():

        form=Merge()
        if form.validate_on_submit():
                for entry in form.item_description.entries:
                        newdata = entry.data
                        return redirect(url_for('test1', newdata=newdata))
        return render_template(
        'test.html', title='test', form=form)



@app.route('/test1', methods=['GET', 'POST'])
def test1():
        newdata=request.args.get('newdata', '')
        return render_template(
        'test1.html', title='test1', newdata=newdata)

In the test1.html template I'm simply calling newdata like so:

{{ newdata }}

It only displays the data from the first inputted line of the form, not the others. Can you see why I am only managing to capture the first entry in the form?

Update:

When I try printing the data after validating the form, it seems the data in the form DOES contain all of the data input into the form:

@app.route('/test', methods=['GET', 'POST']) def test():

    form=Merge1()
    #item_description = form.item_description.data or ''
    if form.validate_on_submit():
            print (form.item_description.data)
            for entry in form.item_description.entries:
                    newdata = entry.data
                    return redirect(url_for('test1', newdata=newdata))
    return render_template(
    'test.html', title='test', form=form)

^ this suggests the form entries are capable of storing data. However.....

When I try to print the entries after the form is validated, nothing gets printed:

@app.route('/test', methods=['GET', 'POST']) def test():

    form=Merge1()
    #item_description = form.item_description.data or ''
    if form.validate_on_submit():
            print (form.item_description.entries)
            for entry in form.item_description.entries:
                    newdata = entry.data
                    return redirect(url_for('test1', newdata=newdata))
    return render_template(
    'test.html', title='test', form=form)

How can I use this to create a dictionary with all of the data in?

1
See my updated answer. - mekicha

1 Answers

1
votes

You need to render the TrackForm class fields as well in your template. That means calling description() instead of description

<form method="POST" >
    {{ form.hidden_tag() }}
    {{ form.item_description() }}
  <input class="btn btn-primary" style="margin-left:300px;"type="submit" value="Submit" />

And instead of doing this:

for entry in form.item_description.entries:
                        newdata = entry.data

You can use a dict instead:

form=Merge()
    if form.validate_on_submit():
            newdata = {}
            for entry in form.item_description.entries:
                    newdata[entry] = entry.data