1 year ago

#379581

test-img

Felipe Carneiro

Using GET/POST method in shopping cart using two databases

I'm creating an e-commerce site using django, the product data is divided into 2 databases product(products) and size(product size), but I can't make it send the data from the size database to the shopping cart, it runs the program well only if it uses the product database.

To try to add the size bank I added the lines var _sizeId=$(".size-id-"+_index).val(); 'id':_sizeId, in the javascript file.

$(document).ready(function(){
// Add to cart
$(document).on('click',".add-to-cart",function(){
var _vm=$(this);
var _index=_vm.attr('data-index');
var _qty=$(".product-qty-"+_index).val();
var _productId=$(".product-id-"+_index).val();
var _sizeId=$(".size-id-"+_index).val();
var _productImage=$(".product-image-"+_index).val();
var _productTitle=$(".product-title-"+_index).val();
var _productPrice=$(".product-price-"+_index).val();
var _sizeSize=$(".product-size-"+_index).val();
// ajax
$.ajax({
url:'/add-to-cart',
date:{
'id':_productId,
'id':_sizeId,
'image':_productImage,
'qty':_qty,
'title':_productTitle,
'price':_productPrice,
'size':_sizeSize,
},
dataType:'json',
beforeSend:function(){
_vm.attr('disabled',true);
},
success:function(res){
$(".cart-list").text(res.totalitems);
_vm.attr('disabled',false);
}
});
// End
});
// End

});

In the view.py file structure I added 'size': request.GET['size'],

def add_to_cart(request):
# del request.session['cartdata']
cart_p = {}
    # when requesting the id requested that it get specific data from the id you selected
    #like image,title,qty and price
cart_p[str(request.GET['id'])] ={
'image': request.GET['image'],
        'title': request.GET['title'],
'qty': request.GET['qty'],
'price': request.GET['price'],
        'size': request.GET['size'],
}
if 'carddata' in request.session:
            #if the request is a valid id, it will call the cartdata variable
if str(request.GET['id']) in request.session['cartdata']:
cart_data = request.session['cartdata']
cart_data[str(request.GET['id'])]['qty'] = int(
cart_p[str(request.GET['id'])]['qty'])
cart_data.update(cart_data)
request.session['cartdata'] = cart_data
else:
cart_data = request.session['cartdata']
cart_data.update(cart_p)
request.session['cartdata'] = cart_data
else:
request.session['cartdata'] = cart_p
return JsonResponse({'data': request.session['cartdata'], 'totalitems': len(request.session['cartdata'])})

In the html file it is like this when selecting the button he would have to send the data to the cart.

                  <div class="input-group my-3" style="width:48%;">
                     <input type="number" value="1" class="form-control product-qty-{{product.id}}" class="form-control product-qty-{{size.id}}" id= "productQty" />
                     <div class="input-group-append">
                        <input type="hidden" class="product-id-{{product.id}}" value="{{product.id}}" />
                        <input type="hidden" class="size-id-{{size.id}}" value="{{size.id}}" />
                        <input type="hidden" class="product-image-{{product.id}}" value="{{product.image}}" />
                        <input type="hidden" class="product-title-{{product.id}}" value="{{product.title}}" />
                        <input type="hidden" class="product-size-{{size.id}}" value="{{size.size}}" />
                        <input type="hidden" class="product-price-{{product.id}}" value="{{product.price}}" />
                        <button class="btn btn-outline-dark add-to-cart" data-index="{{product.id}}" data-index="{{size.id}}" type="button" id= "addToCartBtn"><i class="icon-line-shopping-cart"></i>Add Bag</button>
                     </div>
                  </div>

But it returns only finding the data from the product database.

javascript

python-3.x

django-views

django-templates

0 Answers

Your Answer

Accepted video resources