I am building an online shop website model for a CS50 project and I am currently having a trouble with creating a shopping cart.
I can build a very simple model of a cart where I'd redirect a user to some add_to_cart() function on a button click for all the "Buy" buttons on the page.
What I am trying to achieve is to do it without redirecting to another page on every click. For example, I've seen online shops with many items and their according "Buy" buttons on a page, and on a button click I simply see a counter on Cart element increasing, and a user can continue shopping on the page and only go to the Cart page to finalize the purchase when they are done.
I have this block of a template code that I am working with atm trying to solve the required Python, and probably some JS code to go with it:
<form action="/catalog" method="post">
{% for item in goods %}
<li class="list-group-item list-item-no-leftpad">
<div class="item-info-description">
<p><a href="/item?id={{ item.item_id }}">{{ item.brand }} {{ item.model }}</a>
<p>{{ item.short_description }}
</div>
<div class="item-info-offers">
<p>Price: ${{ item.price }}
<p>In stock: {{ item.in_stock }}
<p><button type="submit" class="btn btn-primary" name="buy" value="{{ item.item_id }}">Buy</button>
</div>
</li>
{% endfor %}
</form>