我正在努力从购物篮中取出物品,但是当我单击“删除”按钮时,它将从购物篮中取出所有物品并将其全部删除。我该如何做到这一点,以便该功能仅接受我要删除的商品的ID?
@phones.route('/delete_cart', methods=['GET', 'POST'])
def delete_cart():
items = [j for i in session["cart"] for j in i]
for item in items:
phone = get_phone_by_id(item)
print([phone.id])
session['cart'].remove([phone.id])
session.modified = True
return redirect(url_for('main.phones'))
HTML:
<table class="table table-striped">
<tr><th>Phone Name</th><th>Quantity</th><th>Price</th><th>Total</th><th></th></tr>
{% for key, phone in display_cart.items() %}
<tr>
<td>{{ phone["brand"] }} {{ phone["model"] }}</td>
<td>{{ phone["qty"] }}</td>
<td>£{{ "%.2f" % phone["price"] }}</td>
<td>£{{ "%.2f" % (phone["qty"] * phone["price"])}}</td>
<td><a href="{{ url_for('phones.delete_cart', id=phone.id) }}">Remove</a></td></tr>
{% endfor %}
答案 0 :(得分:0)
在这种情况下,我将在您的路由中添加您的id
参数并直接访问它。
@phones.route('/delete_cart/id', methods=['GET', 'POST'])
def delete_cart(id):
答案 1 :(得分:0)
在上一个问题Removing item from list python之后,方法应如下所示。
@phones.route('/delete_cart/id', methods=['GET', 'POST'])
def delete_cart(id):
phone = next(item for item in session['cart'] if item['id'] == id)
print(phone)
session['cart'].remove(phone)
session.modified = True
return redirect(url_for('main.phones'))
答案 2 :(得分:0)
路线应如下所示:
@phones.route('/delete_cart/<int:id>', methods=['GET', 'POST'])
def delete_cart(id):
the表示id是一个整数变量。