[Django]-Last element in django template list variable

70👍

✅

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

13👍

Use {{ data|join:", " }}, it does exactly what you need.

https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#join

11👍

Or you can try this as well –

{% for d in data %}
    {{ d }} {% if not forloop.last %},{% endif %}
{% endfor %}

have a look at the docs on template for loops

Leave a comment