How to save data by Button Click handler in DJANGO

Let's assume I have a buy button. In my card. On the card, I'm showing some car details. If the user clicks on the buy button the car/product details will be saved into a new Table. How i can implement this with the Button Click handler in DJANGO?

        <div class="card-body">
            <img src="{{object.car_image.url}}" class="card-img-top" alt="...">
            <div class="card-body">
                <h5 class="card-title fw-bold text-uppercase gap-2">
                {{object.car_name}}
                </h5>
            <p class="card-text btn btn-sm btn-light disabled">Brand : {{object.car_brand}}</p>
            <div class="d-flex gap-2">
                <p class="btn btn-sm btn-primary disabled">Quantity :{{object.quantity}}</p>
                <p class="btn btn-sm btn-warning disabled ">Price :{{object.price}}</p>
            </div>
            <p class="card-text">{{object.description }}</p>
            {% if user.is_authenticated %}
                <button class='btn btn-sm btn-success'>Buy Car</button>
            {% endif %}
        </div>
python
<div class="card-body">
  <img src="{{object.car_image.url}}" class="card-img-top" alt="...">
   <div class="card-body">
   <h5 class="card-title fw-bold text-uppercase gap-2">
     {{object.car_name}}
   </h5>
   <p class="card-text btn btn-sm btn-light disabled">Brand : {{object.car_brand}}</p>
    <div class="d-flex gap-2">
    <p class="btn btn-sm btn-primary disabled">Quantity :{{object.quantity}}</p>
    <p class="btn btn-sm btn-warning disabled ">Price :{{object.price}}</p>
  </div>
  <p class="card-text">{{object.description }}</p>
            {% if user.is_authenticated %}
  <form action="{% url 'buy_car' object.pk %}"
                <button class='btn btn-sm btn-success'>Buy Car</button>
  </form>
            {% endif %}
</div>

first you need to create a form after that create a url to take form in form action you need to send that car object primary key which i shown in example. After that you need to create a views which can take the primary key and save the product in buy model .

Back to Top