简体   繁体   English

如何在django模板中输出带换行符的文本?

[英]How to output text with line breaks in a django template?

I have text saved in a mysql record that looks like this. 我将文本保存在一个看起来像这样的mysql记录中。

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 当我在Django模板上输出它时,它会像这样出现

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

Do I have to pre format this in the views or is there a django, templating syntax to achieve this? 我是否必须在视图中预先格式化这个或者是否有django,模板语法来实现这一点? How can I output the text to reflect the way it appears in the database? 如何输出文本以反映它在数据库中的显示方式?

Use linebreaks or linebreaksbr filter: 使用linebreakslinebreaksbr过滤器:

{{ text|linebreaks }}

Or surround the text with <pre>...</pre> . 或者用<pre>...</pre>包围文本。

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

Try to use <br/> 尝试使用<br/>

<br/> is an HTML code. <br/>是一个HTML代码。 Don't forget you are working with HTML using Django. 不要忘记你正在使用Django处理HTML。 Keeping this in mind will solve a lot of beginners trouble. 牢记这一点将解决很多初学者的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM