Reputation: 177
Django template system adds empty lines when displaying a list in for-loop. I am a bit confused trying to render this template:
<h1>My log</h1>
<textarea>
{% for item in items %}
{{ item }}
{% endfor %}
</textarea>
I expected:
<h1>My log</h1>
<textarea>
* message line of text 1
* message
number 2 on multiple lines
* message line of text 3
</textarea>
But I got:
<textarea>
* message line of text 1
* message
number 2 on multiple lines
* message line of text 3
</textarea>
Is there some way to suppress empty lines and get desired result?
Upvotes: 3
Views: 6480
Reputation: 371
I believe you're looking for the {% spaceless %}
tags:
https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#spaceless
<textarea>
{% spaceless %}
{% for item in items %}
{{ item }}
{% endfor %}
{% endspaceless %}
</textarea>
But note that only space between tags is removed so you may need:
<textarea>
{% spaceless %}
{% for item in items %}{{ item }}{% endfor %}
{% endspaceless %}
</textarea>
In fact the following might actually do it for you:
<textarea>
{% for item in items %}{{ item }}{% endfor %}
</textarea>
If your item
elements actually contain line breaks then you will need to strip them in some way before hand, either using a filter or in a model method or something.
Upvotes: 12
Reputation: 177
Simple solution was found:
<textarea>
{% for item in items %} {{ item }}
{% endfor %}</textarea>
Upvotes: 8