Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Line on Django admin Text Field

I am trying to create a blog o django where the admin posts blogs from the admin site. I have given a TextField for the content and now want to give a new line. I have tried using \n but it doesn't help. The output on the main html page is still the same with \n printing in it. I have also tried the
tag and allowed tags=True in my models file. Still the same. All the tags are coming as it is on the html page.

My Django admin form submitted: Screenshot of the admin page

The result displayed in my public template: Screenshot of a template, showing text without any newlines

like image 623
Ashish Dua Avatar asked May 10 '16 08:05

Ashish Dua


2 Answers

You should use the template filter linebreaks, that will convert the reals \n (that means the newline in the textarea, not the ones you typed using \ then n) into <br />:

{{ post.content|linebreaks }}

Alternatively, you can use linebreaksbr if you don't want to have the surrounding <p> block of course.

like image 197
Maxime Lorant Avatar answered Nov 11 '22 07:11

Maxime Lorant


After searching the internet and trying different Django Template Filters, I came across one specific filter, SAFE.

For me, LINEBREAKS filter didn't work, as provided by @Maxime above, but safe did.

Use it like this in your html template file.

{{post.content|safe}}

To have a better understanding of SAFE filter, i suggest reading the documentation.

like image 40
Ashish Dua Avatar answered Nov 11 '22 05:11

Ashish Dua