Coeus
Coeus

Reputation: 2615

django two new-line characters

I'm new to django. I have a plain text with many paragraph which are entered in django admin. This is pain text copied from internet

Example input

A mysterious landscape phenomenon known as fairy circles has been found in the Australian outback. The fairy circles are characterised by a hexagonal organisation of soil gaps between grass vegetation and seen in the landscape from above.
The beautiful sight cannot be spotted from ground level. Until now, fairy circles have only been documented in the arid landscape of Namibia, Africa.

Example output:

A mysterious landscape phenomenon known as fairy circles has been found in the Australian outback. The fairy circles are characterised by a hexagonal organisation of soil gaps between grass vegetation and seen in the landscape from above.


The beautiful sight cannot be spotted from ground level. Until now, fairy circles have only been documented in the arid landscape of Namibia, Africa.

when I use{{ posts.description|linebreaks }} It just gives me One line break (one new-line characters). In my chrome console it should one <br /> But I wanted to have 2 line breaks

I tried using {{ posts.description|linebreaks|linebreaks }} but didn't help

How should I insert 2 line breaks (two new-line characters)?

Any help is appreciated..Thanks in advance

Upvotes: 0

Views: 1716

Answers (1)

zEro
zEro

Reputation: 1263

You could write your own custom tag similar in line with linebreaks (may be even copy some of the functionality). This is very easy but really discouraged - which is the reason it is not present in standard django template filters.

But otherwise, you could even split the string and render as you wish:

For example you could get split description as described here. And then render the description as follows:

{% for line in posts.descirption_as_list %}
{{ line }}<br/><br/>
{% endfor %}

If you really want to go the route of template tag filters:

@register.filter(name="split_by")
def split_by(value, split_by='\n'):
    return value.split(split_by)

And use it as

{% for line in posts.descirption|split_by:"\n" %}
{{ line }}<br/><br/>
{% endfor %}

In the above you don't even need to specify the second argument (the "\n"). That is, you could have used it as below:

{% for line in posts.descirption|split_by %}
{{ line }}<br/><br/>
{% endfor %}

But that won't be very readable now, would it? Choose wisely.

Upvotes: 2

Related Questions