Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using spaceless in django template

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?

like image 395
David542 Avatar asked Jul 07 '12 08:07

David542


1 Answers

{% 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 %}
like image 105
slackjake Avatar answered Oct 14 '22 03:10

slackjake