I use a custom Widget to modify the context for the widget template (add more information).
Context:
'selected': [[3, 9], [3, 4, 7, 6]], 'depth_levels': {0: [{'name': 'Category A', 'value': 3, 'depth': 0, 'parent_id': None, 'children': [4, 9]}, {'name': 'Category B', 'value': 8, 'depth': 0, 'parent_id': None, 'children': []}], 1: [{'name': 'Category A_1', 'value': 4, 'depth': 1, 'parent_id': 3, 'children': [5, 7]}
{% for path in widget.selected %}
{% for selected in path %}
{% for level in widget.depth_levels.forloop.counter0 %}
{{ level }}
{% endfor %}
{% endfor %}
{% endfor %}
First I cycle thru the selected(path) and the internal arrays(selected). I want to use the {{forloop.counter0}} as a key for depth_levels.
The issue: {{widget.depth_levels.forloop.counter0}}
doesn't return anything.
The forloop.counter is not used as index for a list, but it is used as key to a dictionary.
Using '.' to access is the default way in which Django templates access a dictionary* Using '0', '1' - {{widget.depth_levels.0}}
- etc instead of forloop.counter0
it works.
Using a custom template tag again has an issue because the access is inside a for
and can't use {{}}
inside:
{% for level in widget.depth_levels.forloop.counter0 %}
The keys that I need to access depth_levels are from 0 to the length of each array inside the selected
, the 'path' array in the example.
The values in selected arrays tell me later where to add attributes are not related to the keys of depth_levels.
My end goal is to access the name
, value
in the dictionaries that are in the array.