[Django]-ManytoMany Tags in HTML Template

8👍

Since ManyToMany returns a queryset, you need to loop through the queryset.

You can access the queryset this way: {{ listing.tag.all }}

and you can access it this way

{% for tag in listing.tag.all %}
    {{tag.name}}
{% endfor %}

1👍

You should loop through your set of tags:

{% for tag in listing.tag.all %}
   {{ tag.name }}
{% endfor %}

0👍

If you just need __str__ (or equivalent) and a simple join, this will work:

{{ listing.tag.all|join: ", "}}

Reference: https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#join

Leave a comment