Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django manytomany in template format

In one of my models I have a categories field that is a manytomany configuration.

I was wondering how to get the following output in the template.

Category 1, Category 2, Category 3 and Category 4

So basically separate each category with a comma, apart from the last one that then gets replaced with a "and"

like image 656
ApPeL Avatar asked Feb 07 '11 06:02

ApPeL


1 Answers

{% for category in myob.categories.all %}
  {{ category }}
  {% if not forloop.last %},{% else %} and {% endif %}
{% endfor %}
like image 118
Daniel Roseman Avatar answered Oct 01 '22 08:10

Daniel Roseman