How to read and print text with newlines from a Python string (Django) with HTML?

If someone enters a phrase, for example:

I see you driving round town with the girl I love, and I'm like: haiku. 

(there are no blank lines between each line, but the text is written on three separate lines) in the text field on the web page, and then presses a button, which is then saved in the database via Django , and this line is read and printed on the page, as I can make it print on html page with new characters in text?

So instead of typing back: I see you driving round town with the girl I love, and I'm like: haiku.

It will print as:

 I see you driving round town with the girl I love, and I'm like: haiku. 

I know that if I use: (textarea)soAndSo.body(/textarea) , this saves new lines that were in the file when the user entered it initially. How can I get the same effect, but without using textarea blocks?

+8
python html django newline
source share
4 answers

If there are line queue characters \r\n replaced with <br/> , it is definitely an option, you can consider the css white-space property

 #haiku {white-space:pre;} 

Hidden , it is worth noting that the property is surprisingly well supported , even on IE6 +

+4
source share

The output wrapper in <pre> retain the formatting, <pre>soAndSo.body</pre>

A more advanced solution involves turning newlines into HTML paragraphs or line breaks. For this, Django has built-in filters: linebreaks and linebreaksbr can do the trick.

If you want to use them, filter the output as follows: {{ soAndSo.body|linebreaks }}

+2
source share

Use linebreaksbr (or linebreaks ) in your template.

For example:

{{ value|linebreaksbr }}

If the value is Joel\nis a slug , the output will be Joel<br />is a slug .

+2
source share

There are new lines; it just does nothing in HTML.

Use a filter like {{ content|linebreaksbr }}

0
source share

All Articles