I would like to know how to filter out the last element of a list variable from the context object.
{% for d in data %} {{ d }}, {% endfor %}
I don't want to have the ,
after the last element. Thank you.
NOTE: This is just a hypothetical example. I know we can use the join filter to achieve the same thing here
Do you mean -
{% for d in data %} {% if forloop.last %} {{ d }} {% else %} {{ d }}, {% endif %} {% endfor %}
have a look at the django docs on template for loops
Use {{ data|join:", " }}
, it does exactly what you need.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#join
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With