List indices must be integers or slices, not str

def covid(request):

url1="https://api.rootnet.in/covid19-in/stats/latest"

if request.method=='POST':

    form=CityForm(request.POST)

    form.save()

form=CityForm()

cities=City.objects.all()

covid_data=[]

for city in cities:

    p=requests.get(url1.format(city)).json()

    covid_status={

        'total' : p['data']['summary']['total'],

        'city' : city.name,

        'discharged' :p['data']['regional']['discharged'],

        'confirmedCases' : p['data']['regional']['confirmedCasesIndian'],

        

    }

    covid_data.append(covid_status)

context={'covid_data': covid_data,  'form':form}

return render(request, "covid/index.html", context)

just want to know why I am getting this error “list indices must be integers or slices, not str” please help me

Hi!
The error refers to these lines:

From the json returned by the API, p['data']['regional'] returns a Python list of dictionaries, and not a dictionary.

"regional": [
     {
          ....
     },

Hence the error - the program expects a list index or slice but we gave them a dictionary key (string) to work with instead.

Hope that helped!

thank you for your help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.