Updating product quantities in the cart

When users see the cart, they might want to change product quantities before placing an order. We are going to allow users to change quantities from the cart detail page.

Edit the views.py file of the cart application and change the cart_detail view to this:

def cart_detail(request):    cart = Cart(request)    for item in cart:        item['update_quantity_form'] = CartAddProductForm(                          initial={'quantity': item['quantity'],                          'update': True})    return render(request, 'cart/detail.html', {'cart': cart})

We create an instance of CartAddProductForm for each item in the cart to allow changing product quantities. We initialize the form with the current item quantity and set the update field to True so that when we submit ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.