Reputation: 931
I have the following code in my Django template:
{% for matrix_row in request.session.matrix_rows %}
{% for radio in form.matrix_row_one_column_value %}
<li>{{ radio }}</li>
{% endfor %}
{% endfor %}
How can I change the list for the inner for loop as the outer for loop is iterated over? For example, the lists for successive passes of the outer for loop should be as follows:
form.matrix_row_one_column_value
form.matrix_row_two_column_value
form.matrix_row_three_column_value
form.matrix_row_four_column_value
form.matrix_row_five_column_value
form.matrix_row_six_column_value
Any help would be appreciated. Thanks.
Upvotes: 0
Views: 140
Reputation: 12558
If I understand you correctly, what you want to do is something like form[matrix_row]
{% for matrix_row in request.session.matrix_rows %}
{% for radio in form[matrix_row] %}
<li>{{ radio }}</li>
...
That isn't possible in Django templates, so you would need to add your own simple templatetag
for that. Something like this
@register.filter
def keyvalue(dic, key):
"""Use a variable as a dictionary key"""
return dic[key]
And now you can do
{% for matrix_row in request.session.matrix_rows %}
{% for radio in form|keyvalue:matrix_row %}
<li>{{ radio }}</li>
...
Upvotes: 0
Reputation: 724
Why don't you do the work in the view instead of trying to come up with something complicated in the template?
For example, in your Python:
names = ['apple', 'orange', 'carrot']
colors = [ ['red', 'green'], ['orange', 'red'], ['orange', 'yellow'] ]
fruits = zip(names, colors)
And then in your template:
{% for name, colors in fruits %}
<div>
{{ name }} -
{% for color in colors %}
{{ color }}
{% endfor %}
</div>
{% endfor %}
Upvotes: 1