I have a list of tuples which looks something like the following:
answer_guess_list = [('A',), ('B', 'C',), ('A', 'B', 'C', 'D',)]
In my templates, I have a template variable with the same name.
{% for guess_list in answer_guess_list %}
<p>
{% for guess_value in guess_list %}
{{ guess_value }}, # Notice the comma (,)
{% endfor %}
</p>
{% empty %}
<p>Nothing to show.</p>
{% endfor %}
It displays the list as the following:
A,
B, C,
A, B, C, D,
I don't want the terminal commas and rather display it as:
A
B, C
A, B, C, D
I do not know how to achieve that. Could you help please?
This would be one way to do it:
{% for guess_list in answer_guess_list %}
<p>
{% for guess_value in guess_list %}
{{ guess_value }}{% if forloop.counter < guess_list|length %}, {% endif %}
{% endfor %}
</p>
{% empty %}
<p>Nothing to show.</p>
{% endfor %}
Or you could also use the built-in template filter join:
{% for guess_list in answer_guess_list %}
<p>
{{ guess_list|join:", " }}
</p>
{% empty %}
<p>Nothing to show.</p>
{% endfor %}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments