I have a queryset full of weather objects and I need to loop through a custom array to extract specific weather metrics in each of the weather objects in an HTML template for a Django application.
detail.html
<table>
<tr>
{% for weather_object in weather_objects %}
{% for variable in variable_list %} ## variable_list = ['temp', 'humidity']
<td>{{weather_object.variable}}</td>
{% endfor %}
{% endfor %}
</tr>
</table>
Views.py
context = {'weather_objects': weather_objects,
'variable_list': variable_list}
return render(
request,
'webapp/detail.html',
context=context
)
There could be better ways to do this, but honestly, I'm really stumped here. Why can't I loop through the variable list and extract data from weather_object? It's specifically 'weather_object.variable' that just acts nonexistent.
I can do it manually, I can write specifically
<td>{{weather_objects.temp}}</td>
or
<td>{{weather_objects.humidity}}</td>
but I can't automate it in a for a loop. Why?? I have verified that the variables in the variable list are correct and should work. Could it be because the variable is only a string substitute?
{{ weather_object.variable }}will look forweather_object.variable(so an attributed namedvariable), andweather_object['variable'], not forweather_object.temporweather_object['temp']if'temp'is assigned tovariable.