Home > OS >  Best way to use "context" on Django templates for many variables / dictionaries
Best way to use "context" on Django templates for many variables / dictionaries

Time:07-04

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.

CodePudding user response:

But I've understood (maybe wrong) that the "context" from Django's render needs dictionaries.

The context is a dictionary, but it does not need dictionaries as values. You can pass any value to it.

So you can write this as:

campaign_data = []

# code to fill that list with API data (many dictionaries inside)

clicks = sum(data['campaign_clicks'] for data in campaign_data)

context = {
    'campaign_data' : campaign_data,
    'sum_clicks' : clicks
}
return render(request, 'testing.html', context)

and then render this as:

{% for element in campaign_data %} 
    {{ element.campaign_name }}
    {{ element.campaign_budget }}
{% endfor %}

Total number of clicks - {{ sum_clicks }}

CodePudding user response:

Context does indeed need to be in dictionary form, however you are also able to send object instances, such as simple classes or models. Take this example:

class Car:
    colour: str
    owner_names: List[str]

    def __init__(self):
        ...

And you can send an instance as context:

car = Car(..)
return render(request, 'testing.html', {"car": car})

This is particularly useful if you are using models.py to create a database schema; you can send model instances!

What I found best is to create classes and treat them as data types, instead of sending custom built dictionaries in your view. Nesting classes is also really powerful to keep your code clean and organised. Either way, just using dictionaries is not bad, you just need to find a way that works for your project.

  • Related