Add to cart form - lhmisho/django-eCommerce GitHub Wiki

In this part i am going to add cart form with the product detail so that i can add or remove cart from product detail

Including cart form with product_details page

product/product_detail.html

{%include 'base.html'%}

{% block content%}
<div class="container">
    <div class="row">
        <div class="col-12 col-md-6">
            <h1>{{object.title}}</h1>
            <h3>{{"Some Title"|title}}</h3>
            <p>{{object.timestamp}}</p>
            <p>{{object.timestamp|timesince}} ago</p>
            <p>{{object.description|linebreaksbr|safe}}</p>
            {% if object.image %}
                <img src="{{object.image.url}}" alt="image">
            {% endif %}
        </div>
        <!-- adding cart form  -->
        <div class="col-12 col-md-6">
            <!-- here we passing product=object and cart=cart so that we can use both object -->
            {% include 'products/snippets/update_cart.html' with product=object cart=cart%}
        </div>
    </div>
</div>

{% endblock %}

Creating cart_update form

products/snippets/update_cart.html

<form method="POST" action="{%url "cart:update" %}" class="form">
    {% csrf_token %}
    <input type="hidden" name="product_id" value="{{product.id}}">
    {%if product in cart.products.all  %}
        <button type="submit" class="btn btn-link">remove?</button>
    {%else%}
        <button type="submit" class="btn btn-success">Add to cart</button>
    {%endif%}
</form>

Managing views

carts/views.py

from django.shortcuts import render, redirect
from .models import Cart

from products.models import Product
# Create your views here.

# method for create a cart for user.

def cart_home(request):
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    return render(request, 'carts/cart_home.html', {})

# Update view for cart
def cart_update(request):
    print(request.POST)
    # getting the product_id which is sent from the update_cart.html
    product_id  = request.POST.get('product_id')

    # if the product_id is not none then we taking it to product_obj
    if product_id is not None:
        try:
            product_obj = Product.objects.get(id=product_id)
        except DoesNotExist:
            print("product not exists")
            return redirect("cart:home")   # if product is not exists simply just redirect to home
        cart_obj, new_obj = Cart.objects.new_or_get(request)    # we also added this line to products view on get_context_data so that we can add cart to specific product

        # if the product_obj is exists in cart than we can remove
        if product_obj in cart_obj.products.all():
            cart_obj.products.remove(product_obj)
        # if the product_obj is exists in cart than we can add it
        else:
            cart_obj.products.add(product_obj)
        #return redirect(product_obj.get_absolute_url())
    return redirect("cart:home")

In this portion i just updated

# Update view for cart
def cart_update(request):
    print(request.POST)
    # getting the product_id which is sent from the update_cart.html
    product_id  = request.POST.get('product_id')

    # if the product_id is not none then we taking it to product_obj
    if product_id is not None:
        try:
            product_obj = Product.objects.get(id=product_id)
        except DoesNotExist:
            print("product not exists")
            return redirect("cart:home")   # if product is not exists simply just redirect to home
        cart_obj, new_obj = Cart.objects.new_or_get(request)    # we also added this line to products view on get_context_data so that we can add cart to specific product

        # if the product_obj is exists in cart than we can remove
        if product_obj in cart_obj.products.all():
            cart_obj.products.remove(product_obj)
        # if the product_obj is exists in cart than we can add it
        else:
            cart_obj.products.add(product_obj)
        #return redirect(product_obj.get_absolute_url())
    return redirect("cart:home")
⚠️ **GitHub.com Fallback** ⚠️