A
A
Alexey Fischuk2016-05-10 18:11:51
Django
Alexey Fischuk, 2016-05-10 18:11:51

How to extend django cart functionality?

In general, there is a site in python with django and a map module.
I want to implement functionality + 1 product, - 1 product.
HTML

<form class="cart-button" action="/cartminus/" method="GET">
            <input name="product_id" type="hidden" value="{{ item.product.id }}">
            <input name="quantity" type="hidden" value="{{ item.quantity }}">
            <input class="cart-button-plus" type="submit" value="-">
       </form> 
       <div id="cartAm">{{ item.quantity }}</div>
       <form class="cart-button" action="/cartplus/" method="GET">
            <input name="product_id" type="hidden" value="{{ item.product.id }}">
            <input name="quantity" type="hidden" value="{{ item.quantity }}">
            <input class="cart-button-minus" type="submit" value="+">
       </form>

And in python it looks like this
from django.shortcuts import render, render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from cart import Cart
from spares.models import Goods, ExchangeRate
from users.views import getCart
from django.core.urlresolvers import reverse

def cartminus(request):
    product_id = request.GET['product_id']
    quantity = request.GET['quantity'] 
    cart = Cart(request)
    for item in cart:
        if item.product.id == product_id:
           item.quantity = int(quantity) - int(1)
           item.save()
    return HttpResponseRedirect('/trash/')

def cartplus(request):
    product_id = request.GET['product_id']
    quantity = request.GET['quantity'] 
    cart = Cart(request)
    for item in cart:
        if item.product.id == product_id:
           item.quantity = int(quantity) + int(1)
           item.save()
    return HttpResponseRedirect('/trash/')

Doesn't work, when you click on the buttons, just refreshes the page and that's it? Any advice, suggestions, what to read?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Fischuk, 2016-05-10
@fishouk

UPD I solved it

def cartminus(request):
    product_id = request.GET['product_id']
    product = Goods.objects.get(id=product_id)
    quantity = request.GET['quantity'] 
    cart = Cart(request)
    for item in cart:
        if item.product.id == product.id:
           quantity = int(item.quantity) - int(1)
           if quantity == 0:
                item.delete()
           else:
                item.quantity = int(quantity)
                item.save()
    return HttpResponseRedirect('/trash/')

def cartplus(request):
    product_id = request.GET['product_id']
    product = Goods.objects.get(id=product_id)
    quantity = request.GET['quantity'] 
    cart = Cart(request)
    for item in cart:
        if item.product.id == product.id:
           quantity = int(item.quantity) + int(1)
           if quantity == 0:
                item.delete()
           else:
                item.quantity = int(quantity)
                item.save()
    return HttpResponseRedirect('/trash/')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question