Answer the question
In order to leave comments, you need to log in
How to unpack dictionaries in a list in Django?
Hello everyone!)
Help solve the problem.
The following data structure is passed to the template context:
context = [{key:value, key:value},{key:value, key:value,},{key:value,key:value,},{key:value,key:value,}]
{% for menu_dict in menu_dict_list %}
<tr>
{% for key,value in menu_dict%}
<td>{{value}}</td>
{% endfor %}
</tr>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
Would you show the exact code of the view, otherwise your context is equal to the list, and in the template you are already working with some kind of menu_dict_list. But suppose you have something like:
def some_view(request):
ctx = {
'menu_dict_list': [{'key11': 1, 'key12': 2}, {'key21': 3, 'key22': 4}, {'key31': 5, 'key32': 6}]
}
return render(request, 'some_template.html', ctx)
{% for d in menu_dict_list %}
{% for k, v in d.items %}
{{ k }} - {{ v }}<br>
{% endfor %}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question