marv8569
marv8569

Reputation: 47

Django iterate through a dictionary and render to a template

This code gives me the output that I need on the terminal but when I try to render it in the template it gives me only the last result. I read a couple of other answers but nothing leads me to understand where is the mistake. Thank you.

def forecast(request):
    if request.method == 'POST':
        city = urllib.parse.quote_plus(request.POST['city'])

        source = urllib.request.urlopen('http://api.openweathermap.org/data/2.5/forecast?q='+ city +'&units=metric&appid=ab3d24d85590d1d71fd413f0bcac6611').read()

        list_of_data = json.loads(source)
        pprint.pprint(list_of_data)

        forecast_data = {}

        for each in list_of_data['list']:

            forecast_data['wind_speed'] =  each["wind"]['speed']
            forecast_data['wind_gust'] =  each["wind"]['gust']
            forecast_data['wind_deg'] =  each["wind"]['deg']
            forecast_data["coordinate"] =  str(list_of_data['city']['coord']['lon']) + ', ' + str(list_of_data['city']['coord']['lat'])
            forecast_data["name"] =  list_of_data["city"]["name"]
            forecast_data["dt_txt"] =  each["dt_txt"]

            # uncomment for see the result in the terminal
            print(forecast_data)


        fore = {'forecast_data':forecast_data}


    else:
        list_of_data = []
        fore = {}

    return render(request, "WindApp/forecast.html",fore)

and this is the HTML part:

<div class="row">
  {% for x,y in forecast_data.items %}
  <div class="col d-flex justify-content-center" ">
    <div id="output" class=" card text-black bg-light mb-6">
    <div id="form" class=" card-body">
      <h4><span class="badge badge-primary">City:</span> {{forecast_data.name}}</h4>
      <h4><span class="badge badge-primary">Coordinate:</span> {{forecast_data.coordinate}}</h4>
      <h4><span class="badge badge-primary">Day/Time:</span> {{forecast_data.dt_txt}}</h4>
      <h4><span class="badge badge-primary">Wind Speed: </span> {{forecast_data.wind_speed}} Kt</h4>
      <h4><span class="badge badge-primary">Wind Gust : </span> {{forecast_data.wind_gust}} Kt  <img src="{% static 'img/wind.png' %}" width="64" height="64" alt="wind"></h4>
      <h4><span class="badge badge-primary">Wind direction : </span> {{forecast_data.wind_deg}}  <img src="{% static 'img/cardinal.png' %}" width="64" height="64" alt="dir"></h4>
    </div>
  {% endfor %}

Upvotes: 0

Views: 121

Answers (2)

Mark R.
Mark R.

Reputation: 1323

You're iterating over a list of forecasts, but overwriting a single dictionary with the last one you see in the loop.

What you want is to accumulate the parsed results in a list of dictionaries, such as:

   all_forecasts = []
   for each in list_of_data['list']:
        forecast = {}
        forecast['wind_speed'] =  each["wind"]['speed']
        # ... etc .. all other fields
        all_forecasts.append(forecast)
   fore = {'forecast_data': all_forecasts}

Later on, in the template, you'll need to iterate over forecast_data and render a row for each one:

{% for forecast in forecast_data %}
    <div class="row">
      <div><span>City:</span><span>{% forecast.city %}</span></div>
      <!-- etc the other fields -->
    </div>
{% endfor %}
   

Upvotes: 2

Shamsiddin Parpiev
Shamsiddin Parpiev

Reputation: 99

Write custom filter in directory templatetags

from django import template

register = template.Library()


@register.filter
def get_list(querydict, item):
    return querydict.getlist(item)

then import it in template and use like this

{% load get get_list range %}
{% for x,y in forecast_data|get_list:"items" %}

Reason: if you have items in context I mean not in forecast_data Jinja can understand it, but if you have nested object, I mean list type (in your case items) in some other object Jinja cant access list

Upvotes: 0

Related Questions