Django loop - remove last comma

I have the following loop setting, but you need to remove the comma on the last element (in order to replicate the JSON array for loop2)

{% for product_in_series in series.get_products %}{%spaceless%} {% with product_in_series.product as product %} {%if not forloop.first%} "<img src='{% version product.get_overview 'page_image' %}'>", {%endif%} {% endwith %} {%endspaceless%}{% endfor %} 

Cheers r

+6
source share
2 answers

How about this?

 {% for product_in_series in series.get_products %}{%spaceless%} {% with product_in_series.product as product %} {%if not forloop.first%} "<img src='{% version product.get_overview 'page_image' %}'>" {%if not forloop.last%},{%endif%} {%endif%} {% endwith %} {%endspaceless%}{% endfor %} 
+12
source
 {{ forloop.last|yesno:",&#44;"|safe }} 

&#44; is a comma

+1
source

All Articles