Django tip, join in template

Here is my Django tip of the week (not provided every week 🙄)

Let’s join in template !

Using template filter join

{{ value|join:" // " }}

Works well or a list of string

Django doc : https://docs.djangoproject.com/en/4.2/ref/templates/builtins/#join

Using loop

{% for obj in object_list %}
  {{ obj }}{% if not loop.last %}<br>{% endif %}
{% endfor %}

Will display the __str__ object’s method. You can call any other method of the object

This methods works for both main template engine : Django template and Jinja2

Leave a Reply

Your email address will not be published. Required fields are marked *