R
R
Raccon082021-08-24 12:47:28
Python
Raccon08, 2021-08-24 12:47:28

It throws an error list indices must be integers or slices, not str on line 13, what's the problem?

I'm just starting to learn Python. I'm trying to make a 5 day forecast using the OpenWeathrMap API to display it on Django's localhost. Gives an error list indices must be integers or slices, not str on line 13. And how to implement the issue of weather on OWM for every day, and not every 3 hours.

import requests
from django.shortcuts import render

def index(request):
    appid = 'd2c37acd30116d7d66777e643a1603b9'
    url = 'https://api.openweathermap.org/data/2.5/forecast?q={}&units=metric&cnt=1&appid=' + appid

    city = 'Москва'
    res = requests.get(url.format(city)).json()

    city_info = {
        'city' : city,
        'temp' : res["list"]["main"]["temp"],
        'icon' : res["list"]["weather"]["icon"]
    }

    context = {'info' : city_info}

    return render(request, 'weather/index.html', context)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-24
@Raccon08

You obviously messed up something with the res data structure - at some level it contains a list, and you expect a dictionary with string keys. Print this structure with pprint , figure out how it differs from your expectations, and fix the res call code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question