Adding Items To Shopping Cart Django Python

Viewed 28029

EDIT When I click add to cart this is the output to the terminal

[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

I have two function one adds items to cart and one removes.

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.

I am creating my URL like this,

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'),

The the link to add items to the cart is in a base.html file and looks like this,

  {% if request.user.is_authenticated %}
         <span class="storefront_add_to_cart">
             <a href="{% url 'add_to_cart' book.id %}">[Add to Cart]</a>
         </span>
         {% endif %}

So when you add or remove an item from the cart it calls the respective functions.

Views.py

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')

When I click add to cart the url looks like this (which gives me the error)

http://localhost:8000/store/add/4

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.)

http://localhost:8000/store/cart/%7B%20url%20'remove_from_cart'%20item.book.id%20%%7D

And in case it helps here is the respective models in models.py

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 > 1:
                preexisting_order.quantity -= 1
                preexisting_order.save()
            else:
                preexisting_order.delete()
        except BookOrder.DoesNotExist:
            pass
3 Answers
Related