0

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.

Piyush Dive
  • 162
  • 1
  • 11
  • 1
    Not a comprehensive answer, but it seems like what you really need/want is for your prices to be kept as integers/floats in your models, since they really are numbers that you need to do math with, and then use template formatting to display them as you'd like the user to see them, ie https://stackoverflow.com/questions/346467/format-numbers-in-django-templates – Brendano257 Jul 13 '21 at 14:49
  • hey man!, ya saved me. Thanks for the question link. – Piyush Dive Jul 13 '21 at 14:56

0 Answers0