Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing the dictionary value out in Django template

Tags:

django

I am passing a two dictionaries called student_ids and rows to a Django template. The dictionary student_ids has a key called student_id and invite_id and I would like to print out the student_ids[student_id] to get the invite_id.

The 4th element in row in rows dictionary is the same as student_id. When I try the code below, it doesn’t work…

Django template:

{% for row in rows %}
    <a href="#invite"
     data-invite="{{ invite_ids.row.4 }}">
     {{ row.2 }}
    </a>
{% endfor %}

What should I do to get the correct data out in the Django template? I essentially need student.ids[row[4]] to be printed out, and for it to be simple…

like image 368
lakshmen Avatar asked Aug 29 '12 07:08

lakshmen


2 Answers

The dictionary student_ids has a key called student_id and invite_id and I would like to print out the student_ids[student_id] to get the invite_id.

Try this:

{% for sid, iid in student.ids.items %}
    {{ sid }} -- {{ iid }}
{% endfor %}

Btw: your dictionary name is wierd and confusing because of that dot. I would change it to studentIds or something ...


Updated based on your edits:

First check this: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

I really dont understand exactly what you are after but if you want to access a value from the rows dict based on a key which is equal to a student_id you can try this and see how it works out for you and adjust it to your needs:

{% for student_id, student_id_value in student.ids.items %}
     {{ student_id }}
     {{ rows.student_id }}
{% endfor %}
like image 95
marius_5 Avatar answered Nov 08 '22 09:11

marius_5


Solved it this way:

{% for t_id, i_id in teacher_invite_ids.items %}
 {% if t_id == row.4 %}
  {{ i_id }}
 {% endif %}
{% endfor %}
like image 22
lakshmen Avatar answered Nov 08 '22 10:11

lakshmen