0

I pass to the template:

testruns, which is "get_list_or_404(TestRun)"

and

dict, which is smth like this:

for testrun in testruns:
    dict[testrun.id] = {
        'passed' : bla bla,
        'failed' : bla bla 2
    }

Practically a map between testrun.id and some other info from a set from TestRun Model

In the template I want to do this:

{% for testrun in testruns %}

     console.log("{{ dict.testrun.id }}");
{% endfor %}

But doesn't output anything


console.log("{{ testrun.id }}"); will output a specific id ("37" for example)

console.log("{{ dict.37 }}"); will output the corresponding value from the dict

So, why doesn't this output anything?

console.log("{{ dict.testrun.id }}");

How should I get the data from 'passed' and 'failed' from the dict:

Also, this:

console.log("{{ dict[testrun.id] }}");

Will output this error:

TemplateSyntaxError at /path/dashboard

Could not parse the remainder: '[testrun.id]' from 'dict[testrun.id]'
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Vlad
  • 997
  • 1
  • 5
  • 18

1 Answers1

0

The dot will be considered as a trigger for attribute lookup by template engine, so dict.testrun.id will be parsed as "try to find id attribute from testrun attribute from dict". Instead, if you want to show the whole dict content, you might just iterate through dictionary:

{% for key, value in dict.items %}
    Testcase: {{ key }}
    Passed: {{ value.passed }}
    Failed: {{ value.failed }}
{% endfor %}

Or, if you are looking for dict lookup by variable's value, you will have to make custom template tag, like it was described here - Django template how to look up a dictionary value with a variable

Community
  • 1
  • 1
Serj Zaharchenko
  • 2,621
  • 1
  • 17
  • 20