Answer the question
In order to leave comments, you need to log in
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>
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/')
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question