学習目的で Flask を使用してカートを作成しています。私は peewee(ORM) と WTForms で SQLite を使用することにしました。データベースのアイテムを説明と画像とともに表示するように設定しました。数量を要求するフォームがあり、アイテムとその数量をサイドバーに追加する必要があります。
問題
数量を入力して「追加」を押すと、すべての数量フィールドにその数値が入力され、データベースの最初のアイテムの名前がその数量でサイドバーに投稿されます。
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)
フォーム.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>