Well, inside the loop values
is the inner dict. So you can extract the values you need from there:
{{ category }} ({{ values.info }}) - {{ values.cats|join:", " }}
سؤال
Could someone please help me with django template system. This is my setup in views:
html_vars = {
'some_var1': 'some_val1',
'some_var2': 'some_val2',
'cat': {
't_cat21' : { 'cats': ['val21_1', 'val21_2', 'val21_3'], 'info': 'text21' },
't_cat22' : { 'cats': ['val22_1', 'val22_2', 'val22_3'], 'info': 'text22' },
't_cat23' : { 'cats': ['val23_1', 'val23_2', 'val23_3'], 'info': 'text23' },
},
}
def home(request):
render_to_response('home.html', html_vars)
I want to get this result in html:
t_cat21 (text21) - val21_1, val21_2, val21_3
t_cat22 (text22) - val22_1, val22_2, val22_3
t_cat23 (text23) - val23_1, val23_2, val23_3
But I'am having problems at reading data from django template. I understand the starting point:
{% for category, values in cat.items %}
{{ category }}
???
{% endfor %}
And then I'm stuck (
UPDATE
Thanks for help. Unfortunately cannot vote the answer yet.
المحلول
Well, inside the loop values
is the inner dict. So you can extract the values you need from there:
{{ category }} ({{ values.info }}) - {{ values.cats|join:", " }}
نصائح أخرى
Try this:
{% for category, item in cat.items %}
{{ category }} ({{ item.info }}) - {% for val in item.cats %}{{ val }} {% endfor %}
{% endfor %}
The dot notation will, in this order, try a dictionary lookup, an attribute lookup and a list-index lookup. That means item.info
will be your info text, and item.cats
will be your list of values.