I'm trying to add some context to my ListView context and access them in one for loop with zip like this
class WeatherListView(ListView):
"""
List view of Weather data
"""
template_name = "frontend/weather_list.html"
model = Weather
def get_context_data(self, **kwargs):
weather_query = Weather.objects.all()
temp_list = list(weather_query.values_list('temperature', flat=True))
humidity_list = list(weather_query.values_list('humidity', flat=True))
temp_list_compared = compare_item_to_previous(temp_list)
humidity_list_compared = compare_item_to_previous(humidity_list)
data = super().get_context_data(**kwargs)
context = {
"object_list": zip(data, temp_list_compared, humidity_list_compared)
}
return context
Then I want to get my data in the template for loop
{% for i in object_list %}
{{ i.0.some_field_in_original_context }}
{{ i.1 }}
{{ i.2 }}
{% endfor %}
But what I end up having for my original context {{ i.0 }} is this
paginator
page_obj
is_paginated
How can I still access my original ListView data after putting it in a zip.
__
Update:
Got it I needed to zip object_list inside the original context ListView context looks like this:
{'paginator': None, 'page_obj': None, 'is_paginated': False,
'object_list': <QuerySet [<Weather: 2021-04-06 14:34:32.895936+00:00>,
<Weather: 2021-04-06 20:40:00.304090+00:00>,
<Weather: 2021-04-07 04:24:39.292096+00:00>]>,
'weather_list': <QuerySet [<Weather: 2021-04-06 14:34:32.895936+00:00>,
<Weather: 2021-04-06 20:40:00.304090+00:00>,
<Weather: 2021-04-07 04:24:39.292096+00:00>]>,
'view': <frontend.views.WeatherListView object at 0x7f4ec824b3d0>}
my new context is:
context = {
"object_list": zip(data["object_list"], temp_list_compared, humidity_list_compared)
}
Got it I needed to zip object_list inside the original context ListView context looks like this:
{'paginator': None, 'page_obj': None, 'is_paginated': False,
'object_list': <QuerySet [<Weather: 2021-04-06 14:34:32.895936+00:00>,
<Weather: 2021-04-06 20:40:00.304090+00:00>,
<Weather: 2021-04-07 04:24:39.292096+00:00>]>,
'weather_list': <QuerySet [<Weather: 2021-04-06 14:34:32.895936+00:00>,
<Weather: 2021-04-06 20:40:00.304090+00:00>,
<Weather: 2021-04-07 04:24:39.292096+00:00>]>,
'view': <frontend.views.WeatherListView object at 0x7f4ec824b3d0>}
my new context is:
context = {
"object_list": zip(data["object_list"], temp_list_compared, humidity_list_compared)
}