Add to cart when button clicks django

<form method="post" action="" class="w-full">

{% csrf_token %}

Quantity:

Add to cart

[View cart]()

class Cart(models.Model):

cart_id = models.CharField(primary_key=True)

total = models.DecimalField(max_digits=9, decimal_places=2)

quantity = models.IntegerField()

user = models.OneToOneField(User,on_delete=models.CASCADE)

class CartItem(models.Model):

cart = models.ForeignKey(Cart, on_delete=models.CASCADE)

product = models.ForeignKey(Product, on_delete=models.CASCADE)

quantity = models.IntegerField(default=0)

user = models.OneToOneField(User,on_delete=models.CASCADE)

this is my models.py and detail.html, how to perform view when add to cart button clicks or use do i need to use request.method

  1. List item

Welcome @kumarns !

Side Note: When posting code here, enclose the code between lines of three
backtick - ` characters. This means you’ll have a line of ```, then your code,
then another line of ```. This forces the forum software to keep your code
properly formatted. (I have taken the liberty of correcting your original post.
Please remember to do this in the future.)

A Django view is executed when an HTTP request is issued to the server. If you want a button click in your browser to execute a view without doing a full page refresh, you need to have your JavaScript execute an AJAX-style request to the server for the URL assigned to the desired view.

1 Like

Thanks @KenWhitesell