Holly Johnson Holly Johnson - 1 year ago 145
Python Question

posting Flask wtforms in loop and saving data

I have a Flask-WTF form fields in for loop. I want to post the new quantities for each item.

I am reading about field list. I still dont understand but i think they might be the answer.

def checkout():
form = CartForm()
for item in current_user.cart:
product = Product.query.get(item.product_id)
cart_items.append({product: item.quantity})

return render_template('checkout.html',cart_items=cart_items,form=form)

{% for item in cart_items %}
{% for product, quantity in item.items() %}
{{form.quantity }}
{% endfor %}
{% endfor %}

Problem1: When looping over each Each Flask-WTF form field has the same name.

The output

<select id="quantity" name="quantity"><option value="1">1</option></select>
<select id="quantity" name="quantity"><option value="1">1</option></select>

problem2: how save in the backed if each form has a different name.

Answer Source

I have the same problem exactly! But I solve it without Flask-WTF, the solution below was based on my app. I have an album edit page, I need to loop a text input for each picture in album, then save the text for each picture.

I loop the input in HTML, notice I set the action value for another view function and use each photo's id as each text input's name:

<form action="{{ url_for('edit_photo', id=album.id) }}" method="POST">
    {% for photo in photos %}
        <img class="img-responsive portrait" src="{{ photo.path }}" alt="Some description"/>
        <textarea name="{{ photo.id }}" placeholder="add some description" rows="3">{% if photo.description %}{{ photo.description }}{% endif %}</textarea>
    {% endfor %}
<input class="btn btn-success" type="submit" name="submit" value="submit">

Then I loop the picture and save the input data (get it's value by it's id):

@app.route('/edit-photo/<int:id>', methods=['GET', 'POST'])
def edit_photo(id):
    album = Album.query.get_or_404(id)
    photos = album.photos.order_by(Photo.order.asc())
    if request.method == 'POST':
        for photo in photos:
            photo.about = request.form[str(photo.id)]
        return redirect(url_for('.album', id=id))
    return render_template('edit_photo.html', album=album, photos=photos)