lazybird / django-carton

A simple and lightweight application for shopping carts and wish lists.
Other
274 stars 101 forks source link

Please add example using Ajax to edit quantities or delete #26

Open beckastar opened 8 years ago

beckastar commented 8 years ago

I apologize. I don't know where else to go for support, and I am trying to figure out how to pass an id from my cart to my view so that I can edit quantities or delete an item by clicking the button. It would be so great to see an example of this with Ajax. I've asked a SO question here

joaovitorsilvestre commented 8 years ago

To pass the id to template, you can just do the following:

#views.py
from carton.cart import Cart
def show(request):
    cart = Cart(request.session)
    response = []
    for item in cart.items:
       response.append({'name': item.product.name,'id': item.product.custom_id, 'price': item.subtotal})
    return render(request, 'home/show.html', {'items': response})

In your template, in this case 'show.html', do something lick this:

{% for item in items %}
    <p>item name:</p> {{item.name}}
    <p>item id:</p> {{item.id}}
    <p>item price:</p> {{item.price}}
{% endfor %}

In https://github.com/lazybird/django-carton/blob/master/carton/tests/views.py have a function called remove, it's an example of how delete items. Put this function in some view, and make a javascript to send a 'POST' with a 'product_id'. If you need some help with this, just ask. :)

Sorry if i get something wrong, but that was what I understand about your question.