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’
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.
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.