Django display text fields

I have a textfield with textfield in Django, and users enter texts line by line. When I look at entries in the admin panel, I saw exactly how the user wrote it, but when I display it on my website, in Django templates, it simply joins the sentences, and I could no longer display the text line by line.

To illustrate, the user enters text as

-bla

-bla1

-bla2

However, on my page it looks like

-bla1-bla2-blÀ3

This is my code in the template:

 <div> <p> {{ rev.myTextField }} </p> </div> 
+4
source share
2 answers

In HTML, newlines between text do not imply a newline; you need to use HTML to insert a new line. The easiest way is the <br /> tag, although there are other methods.

Django has a template filter for this: use |linebreaks . Here is the documentation .

So change:

 {{ rev.myTextField }} 

in

 {{ rev.myTextField|linebreaks }} 
+14
source

Try the "safe" tag

 {{ rev.myTextField|safe }} 
+1
source

Source: https://habr.com/ru/post/1382175/


All Articles