How to output text from database with line breaks in a django template?

人走茶凉 提交于 2019-12-05 10:47:46

问题


I have text saved in a database record that looks like this.

This is the text

This is on a new line with a space in between

When I output it on the Django template, it comes out like this

This is the text This is on a new line with a space in between

How can I output the text on my django template to reflect the way it appears in the database?


回答1:


Use linebreaks or linebreaksbr filter:

{{ text|linebreaks }}

Or surround the text with <pre>...</pre>.

<pre>{{ text }}</pre>



回答2:


I linebreaks uses <p> tag to convert new line, this will probally not preserve the empty line or you will just not see it due to css styling. You can try instead linkebrksbr that will use <br/> for new lines.

Hope this helps




回答3:


Try to use <br/>

<br/> is an HTML code. Don't forget you are working with HTML using Django. Keeping this in mind will solve a lot of beginners trouble.



来源:https://stackoverflow.com/questions/22538454/how-to-output-text-from-database-with-line-breaks-in-a-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!