0

I have a dictionary with 3 keys which I am passing from render template as my_dict :

{'quiz_id': ['MT001', 'MT002', 'MT003', 'MT013', 'MT055'], 'quiz_title': ['Place Value 1', 'Place Value 2', 'Addition 1', 'Multiplication', '2D shapes properties'], 'score': ['4', '8', '9', '5', '3']}

In jinja I am trying to iterate though each key to display the values:

    {% for data in my_dict:  %}
        <div class="row border-bottom">
        <div class="col-2 my-3">
            {{ data.quiz_id }} 
        </div>
        <div class="col-7 my-3 text-start">
            {{ data.quiz_title }} 
        </div>
        <div class="col-3 my-3">
            {{ data.quiz_score }} 
        </div>
    </div>    
    {% endfor %}

However, this isn't working and nothing is being displayed, can anyone please suggest how to do this? The dictionary is definitely being passed as it I put {{ my_dict }} in the column the whole dictionary is displayed on the page.

Aristotle
  • 132
  • 1
  • 9
  • 1
    Does this answer your question? [Iterating through Python dictionary with Jinja2](https://stackoverflow.com/questions/50825155/iterating-through-python-dictionary-with-jinja2) – Pranav Hosangadi Nov 21 '22 at 22:26
  • What do you get if you output `{{data}}` in the loop? I have no experience with jinja, but if it's anything like django and regular python, iterating over it will give you its _keys_ in `data`. If you want outputs the way you expect in your template, you need to iterate over a _list_ of dictionaries, where each element of the list is a dictionary containing the `quiz_id`, `quiz_title`, and `quiz_score` keys. – Pranav Hosangadi Nov 21 '22 at 22:28
  • I get keys printing, 'quiz id' in the first column, then a new row with 'quiz_name' in the first column and then 'score' in a new row first column. – Aristotle Nov 21 '22 at 22:36

1 Answers1

0

I couldn't figure this one out so to solve it I created a nested dictionary instead and just iterated it using:

    {% for i in my_dict  %}

        <div class="row border-bottom">
        <div class="col-2 my-3">
            {{ my_dict[i].quiz_id }} 
        </div>
        <div class="col-7 my-3 text-start">
            {{ my_dict[i].quiz_title }} 
        </div>
        <div class="col-3 my-3">
            {{ my_dict[i].score }} 

        </div>
    </div>    
    {% endfor %}
Aristotle
  • 132
  • 1
  • 9