4

我有这个字符串

{{contact.info}}

值为:

name teste\nteste@teste.com.br\n(11) 11111-1111\n(0) 12312-3123\n(12) 12312-3123\n

在 html 中我想看到它:

name teste
teste@teste.com.br<br>
(11) 11111-1111
(10) 12312-3123
(12) 12312-3123

有什么办法吗?

4

2 回答 2

8

使用autoescape模板标签转义 HTML 换行符 ( <br />) 字符。

此外,由于您没有 html 换行符,因此您必须使用linebreaksbrDjango 模板过滤器将文本文件换行符转换为 HTML 换行符,如this answer中所建议的那样。

您的代码应如下所示:

{% autoescape on %}
    {{ contact.info | linebreaksbr }}
{% endautoescape %}
于 2013-07-26T13:20:12.467 回答
4

你想要linebreaks过滤器:

{{contact.info | linebreaks}}

话虽如此,如果contact.info是一个列表会更好 - 然后你可以这样做:

{% for info in contact.info %}
{{ info }}<br />
{% endfor %}
于 2013-07-26T13:26:17.083 回答