Nested variables inside template

Hello,

I am trying to dynamically display a dictionary item based on a variable. The objective is to display a list of environments + the number of processes of each environment. My template looks like this:

       {% for env in  environments %}
            {{ env }} {{ processes_count.{{ env }} }}
       {% endfor %}

however I am getting the following error:
TemplateSyntaxError at /envmanagerapp/
Could not parse the remainder: ‘{{ env’ from ‘processes_count.{{ env’

→ How does Django manage nested variables?

Thank you in advance.

It doesn’t. This is one of those situations where you are likely to need to prepare the data in the view for proper access within the templates.

For more specific assistance, we’d need to see the view preparing this data and the models being referenced, to know what environments and processes_count are, to understand how they need to be referenced.

Here is an extract of the views.py:

    environments = Environment.objects.values_list('envName', flat=True)
    servers = Server.objects.values_list('serverName' , 'serverEnvironment')
    processes = Process.objects.values_list('processServer' , 'processName' , 'processState')
    process_states = Process.objects.order_by().values_list('processState', flat=True).distinct()

    processes_count = defaultdict(dict)
    for env in environments:
        for ps in process_states:
            processes_count[env][ps] = Process.objects.filter(processServer__serverEnvironment=env , processState = ps).count()

    context = {
        'environments': environments,
        'servers': servers,
        'processes': processes,
        'process_states': process_states,
        'processes_count':processes_count,
    }

and here is a dump of the environments , processes_states and processes_count:

{{ environments }}
<QuerySet ['BUILD', 'PROD', 'UAT']>

{{ process_states }}
<QuerySet ['unknown', 'Started', 'Stopped']>

{{ processes_count }}
defaultdict(<class 'dict'>, {'BUILD': {'unknown': 2, 'Started': 1, 'Stopped': 1},
                             'PROD': {'unknown': 2, 'Started': 1, 'Stopped': 1},
                             'UAT': {'unknown': 2, 'Started': 1, 'Stopped': 1}
                             })

What is the output that you’re looking to get from this?

Is it something like:

| ------ | ----------- | -------- |
  Build  |   unknown   |   2
  Build  |   Started   |   1
  Build  |   Stopped   |   1
  Prod   |   unknown   |   2
  Prod   |   Started   |   1
  Prod   |   Stopped   |   1
  UAT    |   unknown   |   2
  UAT    |   Started   |   1
  UAT    |   Stopped   |   1

If not, what is the output you’re trying to create - using this data as an example?

Something like this

Build | unknown: 2 - Started: 1 - Stopped : 1
Prod | unknown: 2 - Started: 1 - Stopped : 1
UAT | unknown: 2 - Started: 1 - Stopped : 1

Side note: To block off a chunk of text (or code) use three backtick - ` characters as lines before and after the text. (A line of ```, then the code/text, then another line of ```.)

So for what you’ve got, it would be something like this:

{% for env, values in processes_count.items %}
    {{ env }} |
   {% for name, count in values.items %}
      {{ name }}: {{ count }} -
   {% endfor %}
   <br/>
{% endfor %}

Obviously, there’s a lot you can do to jazz this up, but it should give you the basic idea.

This also means, that unless you have need for that data elsewhere on the page, you don’t need to pass anything into the context other than processes_count.