0

I am building a cart using Flask for learning purposes. I chose to use SQLite with peewee(ORM) and WTForms. I have it set to display the items from the db with a description and image. I have a form that asks for the quantity then it should add the item and its quantity to the side bar.

The Issue

When You enter a quantity and hit 'add' all the quantity fields will fill with that number and it will post the name of the first item from the database to the sidebar with that quantity.

app.py

@app.route('/cart', methods=['GET', 'POST'])
@login_required
def cart():
    form = forms.PartsSelectForm()
    if request.method == 'POST':
        l_name = Parts.get(Parts.id).part_name
        models.List.create(l_part_name=l_name,
                           l_part_qty=form.quantity.data)
    parts = Parts.select()
    list = List.select()
    return render_template('cart.html', parts=parts, list=list, form=form)

forms.py

class PartsSelectForm(Form):
    quantity = IntegerField()

cart.html

<table class="table">
            <thead>
            <tr>
            <th>Name</th>
            <th>Description</th>
            <th>Image</th>
            <th>Quantity</th>
            <th>Action</th>
            </tr>
            </thead>

            {% for part in parts %}
            <tr>
                <td>{{ part.part_name }}</td>
                <td>{{ part.part_desc }}</td>
                <td style="width: 200px; height:200px;"><img src="/static/img/{{ part.part_img }}" style="max-height:100%; max-width:100%"></td>
                <form method="POST" action="">
                    <td>{{ form.quantity }}</td>

                <td><button type="submit" id="submit" class="btn btn-success">Add</button></td>
</form>
            </tr>
            {% endfor %}
        </table>

1 Answers1

0

You loop over your parts, but you always use form.quantity, which will be the same on every iteration idependently from which "part" you're currently looping over.

  • But how would I have a field for each items quantity if I dont include it in the loop? –  Mar 14 '17 at 22:36