title: Adding Items To Shopping Cart Django Python
body: <p>EDIT
  When I click add to cart this is the output to the terminal </p>
  
  <pre><code>[22/Mar/2016 07:07:44] "GET /store/add/7 HTTP/1.1" 302 0
  [22/Mar/2016 07:07:44] "GET /store/ HTTP/1.1" 200 4888
  </code></pre>
  
  <p>I have two function one adds items to cart and one removes. </p>
  
  <p>I am thinking the problem is my URL request is not being formed right. So I would appreciate someone who is more experienced than me to explaining to me what the expected URL should be. </p>
  
  <p>I am creating my URL like this,</p>
  
  <pre><code>url(r'^add/(\d+)', views.add_to_cart, name='add_to_cart'),  
      url(r'^remove/(\d+)', views.remove_from_cart, name='remove_from_cart'),
      url(r'^cart/', views.cart, name='cart'),
  </code></pre>
  
  <p>The the link to add items to the cart is in a base.html file and looks like this,</p>
  
  <pre><code>  {% if request.user.is_authenticated %}
           &lt;span class="storefront_add_to_cart"&gt;
               &lt;a href="{% url 'add_to_cart' book.id %}"&gt;[Add to Cart]&lt;/a&gt;
           &lt;/span&gt;
           {% endif %}
  </code></pre>
  
  <p>So when you add or remove an item from the cart it calls the respective functions.</p>
  
  <p>Views.py </p>
  
  <pre><code>def add_to_cart(request,book_id):
          if request.user.is_authenticated():
              try:
                  book = Book.objects.get(pk=book_id)
              except ObjectDoesNotExist:
                  pass
              else :
                  try:
                      cart = Cart.objects.get(user = request.user, active = True)
                  except ObjectDoesNotExist:
                      cart = Cart.objects.create(user = request.user)
                      cart.save()
                      cart.add_to_cart(book_id)
                      return redirect('cart')
                  else:
                      return redirect('index')
  
  
  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')
  </code></pre>
  
  <p>When I click add to cart the url looks like this (which gives me the error)</p>
  
  <pre><code>http://localhost:8000/store/add/4
  </code></pre>
  
  <p>When I click remove from cart the url looks like this but I don't get any errors. (The item in the cart stays and will not remove.)</p>
  
  <pre><code>http://localhost:8000/store/cart/%7B%20url%20'remove_from_cart'%20item.book.id%20%%7D
  </code></pre>
  
  <p>And in case it helps here is the respective models in 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 add_to_cart(self, book_id):
          book = Book.objects.get(pk=book_id)
          try:
              preexisting_order = BookOrder.objects.get(book=book, cart=self)
              preexisting_order.quantity += 1
              preexisting_order.save()
          except BookOrder.DoesNotExist:
              new_order = BookOrder.objects.create(
                  book=book,
                  cart=self,
                  quantity=1
                  )
              new_order.save()
  
              def __unicode__(self):
                  return "%s" % (self.book_id)
  
  
      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>