How can I access in a template objects, which are in a list inside a dict?

9 views
Skip to first unread message

MH

unread,
Jun 21, 2020, 2:00:45 PM6/21/20
to Django users
Hi

I have context variable "convar". And this convar consists of a dictionary.

So, it might look like:

convar
|-------- General case
|
|--------- Speical case A
|
|----------Special case B

So, the general case and the special cases are the keys. And there values are lists. And in these lists I have several objects from the database, e.g.

{'General case': [<Object: Object A>, <Object: Object B>, and so on]}

Right now, I am just confused how to get to the single objects in a django template and there fields like Object A.object_field.

I only get the name of the keys of the dictionary by using something like {% for key_var in convar %} {{ key_var}} {% endfor %}

Can you tell me how to correctly get to the values of the objects? :)

Ítalo Silva de Souza

unread,
Jun 21, 2020, 2:54:50 PM6/21/20
to django...@googlegroups.com
You could do something like:

{% for key, value in my_dictionary.items %}
    {{ key }} - {{ value }}
{% endfor %}

or like:

{% for value in my_dictionary.values %}
   {{ value }}
{% endfor %}

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2099c7a7-abdd-42f2-91a3-bd9b2b3b8478o%40googlegroups.com.

MH

unread,
Jun 21, 2020, 4:21:07 PM6/21/20
to Django users
Thank you, I will try that.
Reply all
Reply to author
Forward
0 new messages