Мой HTML код для этого:
Мой HTML код: р>
Код: Выделить всё
{% for key, value in sizes.items %}
{% if key == product.id %}
{{ value.size }}
{% endif %}
{% endfor %}
{% for size in all_sizes %}
{{ size.size }}
{% endfor %}
Код: Выделить всё
$(document).on('click', '.update-cart', function(e){
e.preventDefault();
var product_id = $(this).data('index');
var product_size = $('#size-cart'+ product_id).val();
console.log("Size: ", product_size);
$.ajax({
type: 'POST',
url : "{% url 'cart_update' %}",
data: {
product_id : $(this).data('index'),
product_qty: $('#select' + product_id + ' option:selected').text(),
product_size: product_size,
csrfmiddlewaretoken: '{{ csrf_token }}',
action: 'post'
},
success: function(json) {
location.reload();
},
error: function(xhr, errmsg, err){
}
});
})
Код: Выделить всё
def cart_update(request):
cart = Cart(request)
if request.POST.get('action') == 'post':
print("request POST data:", request.POST)
product_id = int(request.POST.get('product_id'))
product_qty = int(request.POST.get('product_qty'))
size_id = int(request.POST.get('product_size'))
selected_size = get_object_or_404(Product_Size, id=size_id)
print("Selected size: ", selected_size)
cart.update(product=product_id, quantity = product_qty, size=selected_size)
response = JsonResponse({'qty': product_qty})
messages.success(request, ("Your cart has been updated...."))
return response
Код: Выделить всё
def update(self, product, quantity, size):
prdct = get_object_or_404(Product, id=product)
#print(f"Found product: {prdct}")
product_id = str(prdct.id)
product_qty = int(quantity)
print("Size passed in cart.update ", size)
if product_id in self.cart:
self.cart[product_id]['quantity'] = product_qty
if size:
self.cart[product_id]['size']= size.size
print(f"Updated cart: {self.cart[product_id]['size']}")
else:
self.cart[product_id]['size']= None
self.session['cart'] = self.cart
self.session.modified = True
print("session updated: ", self.session.get('cart'))
if self.request.user.is_authenticated:
current_user = Profile.objects.filter(user__id=self.request.user.id)
carty = str(self.cart)
carty = carty.replace("\'", "\"")
current_user.update(old_cart=str(carty))
return self.cart

Я выбрал размер xxl

После нажатия кнопки обновления он снова остается М-32. Это отлично работает для количества. Как это решить?
Подробнее здесь: https://stackoverflow.com/questions/792 ... n-database