# Nested variables inside template

**URL:** <https://forum.djangoproject.com/t/nested-variables-inside-template/26333>\
**Category:** Templates & Frontend\
**Created:** [December 21, 2023, 3:38pm UTC](https://forum.djangoproject.com/t/nested-variables-inside-template/26333 "2023-12-21T15:38:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mhm2047](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mhm2047/32/17908_2.png) [@mhm2047](https://forum.djangoproject.com/u/mhm2047)\
**Post date:** [December 21, 2023, 3:38pm UTC](https://forum.djangoproject.com/t/nested-variables-inside-template/26333/1 "2023-12-21T15:38:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 21, 2023, 3:45pm UTC](https://forum.djangoproject.com/t/nested-variables-inside-template/26333/2 "2023-12-21T15:45:06Z")

</div>

> [@mhm2047](#):
>
> → How does Django manage nested variables?

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.

---

<div class="post-metadata">

**Author:** ![mhm2047](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mhm2047/32/17908_2.png) [@mhm2047](https://forum.djangoproject.com/u/mhm2047)\
**Post date:** [December 22, 2023, 4:10pm UTC](https://forum.djangoproject.com/t/nested-variables-inside-template/26333/4 "2023-12-22T16:10:49Z")

</div>

Here is an extract of the views.py:

```auto
    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`:

```auto
{{ 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}
                             })

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 22, 2023, 4:58pm UTC](https://forum.djangoproject.com/t/nested-variables-inside-template/26333/5 "2023-12-22T16:58:23Z")

</div>

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

Is it something like:

```auto
| ------ | ----------- | -------- |
  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?

---

<div class="post-metadata">

**Author:** ![mhm2047](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mhm2047/32/17908_2.png) [@mhm2047](https://forum.djangoproject.com/u/mhm2047)\
**Post date:** [December 22, 2023, 5:17pm UTC](https://forum.djangoproject.com/t/nested-variables-inside-template/26333/7 "2023-12-22T17:17:52Z")

</div>

Something like this

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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 22, 2023, 5:33pm UTC](https://forum.djangoproject.com/t/nested-variables-inside-template/26333/8 "2023-12-22T17:33:06Z")

</div>

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:

```auto
{% 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`.
