I am building an e-commerce site and I'm working on quantity & price section. What I want to do is, when the user increases the quantity of the product the price should also change depending upon the quantity.
views.py
def show_cart(request):
if request.user.is_authenticated:
user = request.user
cart = Cart.objects.filter(user=user)
amount = 0
shipping_amount = 70
total_amount = 0
cart_product = [p for p in Cart.objects.all() if p.user == user]
if cart_product:
for p in cart_product:
temp_amount = (p.quantity * p.product.discounted_price)
amount = temp_amount + amount
total_amount = amount + shipping_amount
return render(request, 'app/addtocart.html', {'carts':cart, 'amount':amount, 'total_amount':total_amount})
models.py:
I have used CharField as I wanted the products price to look like this: 14,999 instead of 14999 this.
class Product(models.Model):
title = models.CharField(max_length = 100)
selling_price = models.CharField(validators=[validate_comma_separated_integer_list],max_length=200, blank=True, null=True,default='')
discounted_price = models.CharField(validators=[validate_comma_separated_integer_list],max_length=200, blank=True, null=True,default='')
The error is because I used String for the price to be comma separated. Is there any way out of it, so that the price increases with the increase in quantity.