2

I've this piece of code:

{% for a in doctor.treatment.all %}
  <p>  {{a}}  </p>
{% endfor %}

It gives me the output like this:

Teeth Whitening
Braces
Veneers

Is there anyway I can get it like this in the templates in one line with commas?

Teeth Whitening, Braces, Veneers.
James L.
  • 1,143
  • 22
  • 52
  • 1
    in html i have not any idea .... but in python you can put every **doctor.treatment** in a string with a ', ' character end of each ... using **Join**. then send that string to request page – Arash Hatami Dec 31 '15 at 07:32
  • See https://stackoverflow.com/questions/27356191/django-how-to-display-list-of-objects-in-one-line-using-for-loop-in-templates/27356260 – utapyngo Jan 29 '19 at 03:32

2 Answers2

5

See join - https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#join

This should work

<p>
{{ doctor.treatment.all|join:", "  }}.
</p>
furas
  • 134,197
  • 12
  • 106
  • 148
0

<p></p> is a paragraph, it will cause a line break.

I think the following code will work for you:

<p>
    {% for a in doctor.treatment.all %}
        {{a}}{% if a == doctor.treatment.all[-1] %},&ensp;{% else %}.{% endif %}
    {% endfor %}
</p>

join is better.

damnever
  • 12,435
  • 1
  • 13
  • 18