Adding products to the cart

Now, we need to add an Add to cart button to the product detail page. Edit the views.py file of the shop application, and add CartAddProductForm to the product_detail view as follows:

from cart.forms import CartAddProductFormdef product_detail(request, id, slug):    product = get_object_or_404(Product, id=id,                                         slug=slug,                                         available=True)    cart_product_form = CartAddProductForm()    return render(request,                  'shop/product/detail.html',                  {'product': product,                   'cart_product_form': cart_product_form})

Edit the shop/product/detail.html template of the shop application, and add the following form to the product's price as follows:

<p class="price">${{ product.price }}</p><form action="{% url "cart:cart_add" product.id %}" method="post"> ...

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.