问题
I am facing issue while appending fieldlist through append_entry in flask-wtf.
I am able to add one row of fields from fieldlist but after adding first row, i am not able to add second row.
No errors are shown.
In first attempt a new row is added of field list items and in server log "Data is King" and "True" is printed (I have included this to see if add button sends data and loop actually goes through).
In second attemt new row is not added but page reloads and in server log "Data is King" and "True" is printed (This means loop is executed but append_entry command does not work).
I found same question asked 4 years ago, but solution given in that question's accepted answer does not solve the problem.
Below is the link.
https://stackoverflow.com/questions/8637831/cant-append-entry-fieldlist-in-flask-wtf-more-than-one#=
I have tried all the combinations but nothing solves the problem (am upto this problem for more than 48hrs now).
Can someone help please.
form.py Data
class FormCont(FlaskForm):
description = StringField('Description',validators=[DataRequired()])
start_time = FloatField('Start Time',validators=[DataRequired()])
class AddItemCont(FlaskForm):
a = FloatField(validators=[DataRequired()])
b = FloatField(validators=[DataRequired()])
c = FloatField(validators=[DataRequired()])
item_cont = FieldList(FormField(FormCont), min_entries=0, max_entries=100)
add_item_cont = SubmitField("Add")
remove_item_cont = SubmitField("Remove")
submit = SubmitField()
view.py data
@additem.route('/additem/', methods = ['GET','POST'])
def additemcont():
form = AddItemCont(request.form)
if form.add_item_cont.data:
form.item_cont.append_entry()
print ("Data is King")
print (form.add_item_cont.data)
return render_template('entry.html',form=form)
entry.html data
<form action="{{url_for('additemcont')}}" method='POST' name='AddItemCont' class="form-horizontal">
{{ form.hidden_tag() }}
<div class = "row">
<div class="col-md-1" >
<form action="" method="POST" class="form-horizontal"><button type="submit" class="btn btn-success btn-xs" name="add_item_cont" value="add_item_cont"><span class="glyphicon glyphicon-plus" aria-hidden="true"></button>
</form>
</div>
</div>
<div class = "row"><div class="col-md-12" >
{% for x in form.item_cont %}
{% for i in x %}
{{ i (class="form-control") }}
{% endfor %}
{% endfor %}
</div></div>
回答1:
Have you tried initializing the form and passing it to append_entry
?
@additem.route('/additem/', methods = ['GET','POST'])
def additemcont():
form = AddItemCont(request.form)
if form.add_item_cont.data:
### here
form_cont = FormCont()
form.item_cont.append_entry(form_cont)
####
print ("Data is King")
print (form.add_item_cont.data)
return render_template('entry.html',form=form)
来源:https://stackoverflow.com/questions/50895288/append-entry-is-not-working-after-appending-first-time-i-am-using-fieldlist-w