Почему template не выводит данные?

Пробую сделать сайт выводящий погоду. Все данные спокойно передаются в context, да и все успешно сохраняется в дб. Но вот на выводе получается, что просто пару пустых колонок, которые ничего не выводят. С чем это может быть связано?

import requests
from django.shortcuts import render
from .models import City
from .forms import CityForm

def index(request):
    appid='8daaf5ff8c413765ce21a9610fad80cf'  #https://home.openweathermap.org/api_keys
    url ='http://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid=' + appid

    if(request.method == 'POST'):
        form = CityForm(request.POST)
        form.save()

    form = CityForm()

    cities = City.objects.all()

    all_cities =[]

    for city in cities:
        res = requests.get(url.format(city.name)).json()
        city_info = {
            'city': city.name,
            'temp': res['main']['temp'],
            'icon': res['weather'][0]['icon'],
            }
        print(city_info)
        if res.get('main'):
            city_info = {
                'city': city.name,
                'temp': res['main']['temp'],
                'icon': res['weather'][0]['icon'],
                'error': False,
            }
        else:
            city_info= {
                'city': city.name,
                'error': True
            }
        all_cities.append(city_info)



    context = {'all_info': city_info, 'form': form}
    return render(request, 'weather/weather.html', context)

Templates:



    </div>
            <div class="col-5 offset-2">
                <h1>Information</h1>


                {% for info in all_info %}
                <div class="alert alert-info">
                    <div class="row">
                        <div class="col-9">
                        <b>City:</b> {{ info.city }} <br>
                        <b>Temperature:</b> {{ info.temp }}<sup>o</sup> <br>
                        </div>
                    <div class="col-2 offset-1">
                    <img src="http://openweathermap.org/img/w/{{ info.icon }}.png" alt="photo weather">
                        </div>
                    </div>
                </div>
                {% endfor %}
             </div>

Вывод в браузере

Вы скорее всего не верно передаете данные в context. Переменная city_info не содержит то, что вы ожидаете в template.
Моя догадка, что вы используете переменную all_cities, в которую помещаете данные обо всех городах, но при этом в контексте ее нет. Попробуйте так

    context = {'all_info': all_cities, 'form': form}

Переменная all_cities содержит список всех городов со значениями, которые вы перебрали в for-директиве. Передав ее в template вы можете забрать данные

Для отладки используйте print(context) перед return, чтобы увидеть данные, которые предаются на страницу

Вернуться на верх