简体   繁体   English

Python迭代未捕获WTForms数据字段/字段列表中的所有数据

[英]Python iteration not capturing all data in WTForms data fields/fieldlist

I've tried to simplify a previous question I asked earlier . 我试图简化我先前问的上一个问题。 I have a FieldList Form: 我有一个FieldList表单:

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'. 我正在使用下面的方法迭代条目,并希望将它们全部存储在变量“ 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: 在test1.html模板中,我只是像这样调用newdata:

{{ 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(): @ 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(): @ 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? 如何使用它创建包含所有数据的字典?

You need to render the TrackForm class fields as well in your template. 您还需要在模板中呈现TrackForm类字段。 That means calling description() instead of description 这意味着调用description()而不是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: 您可以改用dict

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM