Remove products from cart in django
Based on your comment, this was a clash between two paths: two paths that have certain possible paths in common. In that case, Django will pick the first one of the matching paths, hence the delete_cart
view is never called.
You can further simplify the view logic to:
def delete_cart(request, slug):
Cart.objects.filter(user=request.user, products__slug=products).delete()
return redirect('cart')
furthermore since this is a view that alters entities, this should be done through a POST or DELETE request, not a GET request, so you might want to restrict the view with the @require_http_methods(…)
decorator [Django-doc]:
from django.views.decorators.http import require_http_methods
require_http_methods(['POST', 'DELETE'])
def delete_cart(request, slug):
Cart.objects.filter(user=request.user, products__slug=products).delete()
return redirect('cart')
In that case you thus create a mini form that will make the POST request with:
<form method="POST" action="{% url 'delete-cart' product.slug %}">
{% csrf_token %}
<button type="submit">remove from the cart</button>
</form>
with delete-cart
the name of the path that refers to the delete_cart
view.