I want to send a lot of data to 1 Django template (to show it on 1 specific page).
The data comes from 2 sources:
- At least 1 list, with many dictionaries inside (I can't change any of this, it comes from Google Ads API).
- Many variables (I can change them, for example, to turn them into dictionaries or tuples if necessary).
The variables are very simple, something like "variable = other_variable / 100" (just 1 value, no need for a key), so I don't need the variables to be turned into a dictionary.
But I've understood (maybe wrong) that the "context" from Django's render needs dictionaries.
The variables' data comes from the list (I do some calculations using some of the list values), but maybe this is not relevant for my question.
Right now I'm doing something like this in my views.py:
campaign_data = []
CODE TO FILL THAT LIST WITH API DATA (MANY DICTIONARIES INSIDE)
clicks = sum(data['campaign_clicks'] for data in campaign_data)
sum_clicks = {"sum_clicks" : clicks}
context = {
'campaign_data' : campaign_data,
'sum_clicks' : sum_clicks
}
return render(request, 'testing.html', context)
And then on the Django template I use the data from views.py like this:
{% for element in campaign_data %}
{{ element.campaign_name }}
{{ element.campaign_budget }}
{% endfor %}
{% for key, value in sum_clicks.items %}
{{key}} - {{value}}
{% endfor %}
This works, but it seems like a lot of "extra steps" (create a dictionary I don't really need only to feed it to context, doing a loop for a dictionary that just has 1 key-value...).
And I'm going to use a lot of those variables or "extra dictionaries" (not just 1 like in this example), so it will matter.
Is there an easier and/or more efficient way to do this?
I'm a Python and Django beginner, so I don't really know what I'm doing.