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…
The dictionary
student_ids
has a key calledstudent_id
andinvite_id
and I would like to print out thestudent_ids[student_id]
to get theinvite_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 %}
Solved it this way:
{% for t_id, i_id in teacher_invite_ids.items %}
{% if t_id == row.4 %}
{{ i_id }}
{% endif %}
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With