Alex Rivera | Logout

Using spaceless in django template

Asked 2012-07-07T08:47:14.523
10

I have the following code:

    {% for item in profile.jobs.all %}
        {% if not forloop.first %}, {% endif %}{{ item }}
    {% endfor %}

Which produces something like the following:

"Programmer , Plumber , Philosopher"

I do not want the leading space before the comma, but the only way I've been able to get rid of it is to compress it onto one line, which reduces readability:

{% for item in profile.jobs.all %}{% if not forloop.first %}, {% endif %}{{ item }}{% endfor %}

Is there a better way to deal with this?

Edit
Report

1 Answer

5

{% spaceless %} only strips spaces between html tags.

You can either use {{ value|join:", " }}

or I believe this will work:

{% for item in profile.jobs.all %}
    {% if not forloop.first %}, {% endif %}
    {{ item }}
{% endfor %}
answered 2012-07-07T10:50:31.633

Your Answer