Implementing ‘Add To Cart’ Feature in E-commerce Website using Django | EP. 26[2/2]

Posted by

Add To Cart using Django in E-commerce Website using Django | EP. 26[2/2]

Add To Cart using Django in E-commerce Website using Django | EP. 26[2/2]

In the last episode, we covered how to create a simple e-commerce website using Django. In this episode, we will focus on adding a “Add To Cart” functionality to our website.

Step 1: Create a Model for the Cart

We will start by creating a model for the cart in our Django application. The cart model will have a relationship with the product model and will store the quantity of each product added to the cart.


class Cart(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)

Step 2: Create a View for Adding Products to the Cart

Next, we will create a view that will handle the logic for adding products to the cart. This view will first check if the product is already in the cart, and if so, it will update the quantity. If the product is not in the cart, it will create a new cart item for the product.


def add_to_cart(request, product_id):
product = get_object_or_404(Product, pk=product_id)
cart, created = Cart.objects.get_or_create(product=product)
if not created:
cart.quantity += 1
cart.save()
return redirect('cart')

Step 3: Update the Frontend to Add Products to the Cart

Finally, we will update the frontend of our e-commerce website to allow users to add products to the cart. We will add a button or link next to each product that, when clicked, will trigger the “add_to_cart” view we created in the previous step.


Add to Cart

With these steps, we have successfully added a “Add To Cart” functionality to our e-commerce website using Django. Users can now easily add products to their cart and proceed to checkout.

0 0 votes
Article Rating
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sambabarinde
6 months ago

I've been on this add to cart video for over 4 days now and i still can't fix it, I'll be happy to get help from you please 🙏 😢

@samalizadeh5107
6 months ago

hello i have a bug 🙁 when i wana add the product to cart it doesn`t added, i mean in my console it writes Adding product to cart (bedforeSend in ajax) but it wont success (success function in ajax) ive written all the code same as yours but idk why it doesnt work!

my python:

def add_to_cart(request):

cart_product = {}

cart_product[str(request.GET['product_id'])] = {

'product_title': request.GET['product_title'],

'product_quantity': request.GET['product_quantity'],

'product_price': request.GET['product_price']

}

if 'cart_data_obj' in request.session:

if str(request.GET['product_id']) in request.session['cart_data_obj']:

cart_data = request.session['cart_data_obj']

cart_data[str(request.GET['product_id'])]['product_quantity'] = int(cart_product[str(request.GET['product_id'])]['product_quantity'])

cart_data.update(cart_data)

request.session['cart_data_obj'] = cart_data

else:

cart_data = request.session['cart_data_obj']

cart_data.update(cart_product)

request.session['cart_data_obj'] = cart_data

else:

request.session['cart_data_obj'] = cart_product

return JsonResponse({"data": request.session['cart_data_obj'], 'totalcartitems': len(request.session['cart_data_obj'])})

@preciousimoniakemu4106
6 months ago

The add to cart is not working at the point you said it was not supposed to work yet yours did work

@Johnny_keys
6 months ago

Hi Desphix, my zoom effect does not work, can you help me?

@sonofthesunlight5939
6 months ago

Nice series. Cool ! I will start from zero. Subscribed !

@shaysframe1472
6 months ago

GREAT!! keep up the upload and series really learning a lot!

@yebetesebchewata
6 months ago

I start but, I can't buy Multipurpose E-commerce Platform
Using Django and JS theme can you please provide it for free for me

@eyoditech_offi
6 months ago

how many episode still to finish this project ? cause im thinking to start with you

@sporttyt
6 months ago

You best

@ennygenius8823
6 months ago

Nice one boss