def delete(self,product):
product_id = str(product)
if product_id in self.cart:
del self.cart[product_id]
self.session.modified = True
def cart_delete(request):
cart = Cart(request)
if request.POST.get('action') == 'post':
print('=========')
#get stuff
product_id = int(request.POST.get('product_id'))
print('product_id =============== ', product_id)
cart.delete(product=product_id)
response = JsonResponse({'product':product_id})
return response
(... 생략 ...)
<button type="button" data-index="{{product.id}}" class="btn btn-danger delete-product">삭제</button>
(... 생략 ...)
$(document).on('click', '.delete-product', function (e) {
let productid = $(this).data('index');
$.ajax({
type: 'POST',
url: '{% url "cart:cart_delete" %}',
data: {
product_id: $(this).data('index'),
csrfmiddlewaretoken: '{{ csrf_token }}',
action: 'post',
},
success: function (json) {
console.log(json);
location.reload(); //페이지 새로 고침
},
error: function (e) {
console.log(e);
},
});
});
삭제가 잘되는지 확인
