title: Tracking Items Quantity & Price In Shopping Cart
body: <p>I have a shopping cart in a Django application. You can add and remove items from the cart. </p>
  
  <p>I have finally got my logic to work. Through much trouble shooting it will add items from the cart and remove them. The price is being recalculated each time you add or remove an item from the cart. </p>
  
  <p>But only on the first product in the cart. The top product in the cart readjust the price based on its quantity. The rest of the products can be increased or decreased but the cost of the total always stays the same. I am unclear if this problem is in my logic in the view or model. </p>
  
  <p><a href="https://i.stack.imgur.com/U2nHC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U2nHC.png" alt="enter image description here"></a></p>
  
  <p>Thank you,</p>
  
  <p>models.py</p>
  
  <pre><code>class Cart(models.Model):
      user = models.ForeignKey(User)
      active = models.BooleanField(default=True)
      order_date = models.DateField(null=True)
      payment_type = models.CharField(max_length=100, null=True)
      payment_id = models.CharField(max_length=100, null=True)
  
      def __unicode__(self): 
              return "%s" % (self.user)
  
      def remove_from_cart(self, book_id):
          book = Book.objects.get(pk=book_id)
          try:
              preexisting_order = BookOrder.objects.get(book=book, cart=self)
              if preexisting_order.quantity &gt; 1:
                  preexisting_order.quantity -= 1
                  preexisting_order.save()
              else:
                  preexisting_order.delete()
          except BookOrder.DoesNotExist:
              pass
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>@login_required
  def add_to_cart(request,book_id):
      book = get_object_or_404(Book, pk=book_id)
      cart,created = Cart.objects.get_or_create(user=request.user, active=True)
      order,created = BookOrder.objects.get_or_create(book=book,cart=cart)
      order.quantity += 1
      order.save()
      messages.success(request, "Cart updated!")
      return redirect('cart')
  
  
  def remove_from_cart(request, book_id):
      if request.user.is_authenticated():
          try:
              book = Book.objects.get(pk = book_id)
          except ObjectDoesNotExist:
              pass 
          else:
              cart = Cart.objects.get(user = request.user, active = True)
              cart.remove_from_cart(book_id)
          return redirect('cart')
      else:
          return redirect('index')
  
  def cart(request):
      if request.user.is_authenticated():
          cart = Cart.objects.filter(user=request.user.id, active = True)
          orders = BookOrder.objects.filter(cart=cart)
          total = 0
          count = 0
          for order in orders:
              total += (order.book.price * order.quantity)
              count += order.quantity
              context = {
              'cart': orders,
              'total': total,
              'count': count,
              }
              return render(request, 'store/cart.html', context)
          else:
              return redirect('index')
  </code></pre>