string indices must be integers

TypeError at /cart/addtocart/

string indices must be integers

I got an error here

n=int(cart_p[str(request.GET.get('id'))['qty']])

views.py

rom django.shortcuts import render, redirect, HttpResponse, reverse
import json
from django.http import JsonResponse
from django.template.loader import render_to_string
from django.views import View
from onlinePizza.models import Product

def add_to_cart(request):
	cart_p={}
	cart_p[str(request.GET.get('id'))]={
		'title':request.GET.get('title'),
		'qty':request.GET.get('qty'),
		'price':request.GET.get('price'),
	}

	if 'cartdata' in request.session:
		if str(request.GET.get('id')) in request.session['cartdata']:
			cart_data=request.session['cartdata']
			n=int(cart_p[str(request.GET.get('id'))['qty']])
			cart_data[str(request.GET.get('id'))]['qty']=n
			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'])})

I got none value in result

cart_data {'None': {'price': None, 'qty': None, 'title': None}}

cart_p {'None': {'price': None, 'qty': None, 'title': None}}

I hope this code is help for answer.

The error is pretty straight forward.

The data you think you’re getting in your view isn’t there.

None of those fields are in the request object being submitted by the browser.

The root cause isn’t in this view, it’s being caused by what the browser isn’t submitting.

1 Like

Can you provide solved CODE?
And also Explain in easy way

The problem isn’t in what you’ve posted here.

The problem is in whatever you’re doing in your view / template / JavaScript that is submitting to this view.

That is the area of code that you need to focus on.

1 Like

cart.js

$(document).ready(function(){

	// AddToCart
	$(document).on('click', '.cart-btn',  function(){
		var _vm=$(this);
		var _index=_vm.attr('data-index')
		var _qty=$('.productQty-'+_index).val()
		var _productId=$('.product_id-'+_index).val()
		var _productTitle=$('.product_title-'+_index).val()
		var _productPrice=$('.product_price-'+_index).val()
		// Ajex
		$.ajax({
			url:'/cart/addtocart',
			data:{
					'id':_productId,
					'qty':_qty,
					'title':_productTitle,
					'price':_productPrice
				},
			dataType:'json',
			beforeSend:function(){
				_vm.attr('disabled', true);
			},
			success:function(res){
				$('.cartQty').text(res.totalitems)
				_vm.attr('disabled', false);
			},
		});
	});
	// End

	// Delete item from cart
	$(document).on('click', '.delete-item', function(){
		var _pId=$(this).attr('data-item');
		var _vm=$(this);
		// Ajex
		$.ajax({
			url:'/cart/deleteformcart',
			data:{
					'id':_pId,
				},
			dataType:'json',
			beforeSend:function(){
				_vm.attr('disabled', true);
			},
			success:function(res){
				$('.cartQty').text(res.totalitems)
				_vm.attr('disabled', false);
				$('#cartlist').html(res.data)
			},
		});
	});
	// End

});

HTML

<div class="col-4"><!-- Cart Button -->
         <input type="number" value="1" class="productQty" name="product_Qty" style="width: 35px; height: 35px; padding: 11px; border: 2px solid #FFC222; border-radius: 10px;">
         <input hidden type="text" class="product_id" name="product_id" value="{{i.id}}">
         <input hidden type="text" class="product_price" name="product_price" value="{{i.price}}">
         <input hidden type="text" class="product_title" name="product_title" value="{{i.product}}">
         <button class="main-btn cart-btn" data-index="{{i.id}}" style="padding: 5px 32px">Add <i class="fa-solid fa-cart-shopping"></i></button>
</div><!-- /Cart Button -->

I solve this problem. In HTML input class I don`t add -{{i.id}}

So that’s why I face this problem.

Can you please do one more help?

I want to convert my addtocart button in qty. ‘+’ and ‘-’ buttons using JS.

I hope you provide the code.