0

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

Coeus
  • 2,385
  • 3
  • 17
  • 27
  • You can directly save 2 `
    ` in the `posts.description` (e.g. replace `\n`) and apply the `safe` filter in the template
    – user2390182 Mar 16 '16 at 08:56
  • Thanks for reply..could you please provide a example so that it will be easy to understand... – Coeus Mar 16 '16 at 09:03

1 Answers1

2

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.

Community
  • 1
  • 1
zEro
  • 1,255
  • 14
  • 24
  • 1
    @Coeus a custom filter would help, but not necessary as mentioned. Unless you want to do something really fancy filters are bit of an overburden: to the developers (I think). Why use filter tags for things that can be done by the instance it self? A good reason could be: "I want to reuse this elsewhere (even everywhere)". If that's the case, by all means go for it. – zEro Mar 16 '16 at 09:17