Home > Blockchain >  Django show list of strings in textarea, each string from new line
Django show list of strings in textarea, each string from new line

Time:08-15

I have a list lst=['apple', 'pear', 'peach'].

I want to show each item in textarea from new line (removing redundant subspaces).

            <textarea>
                    {% for item in lst %}
                        {{ item }}
                        {% if not forloop.last %}<br/>{% endif %}
                    {% endfor %}
            </textarea>

This code doesn't work. <br/>s are shown as they are. And there are redundant whitespaces at the beginning of the textarea and between the items.

CodePudding user response:

The <textarea> tag is very stupid:) in your template there is no place for new line and space:

<textarea>{% for item in lst %}{{ item }}{% if not forloop.last %}&#13;&#10;{% endif %}{% endfor %}</textarea>

It need to be in one line or it will break.

&#10; Line Feed and &#13; Carriage Return as @Bukudan explain in this thread: https://stackoverflow.com/a/8627926/13773284

  • Related