我有这个字符串
{{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
有什么办法吗?
我有这个字符串
{{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
有什么办法吗?
使用autoescape
模板标签转义 HTML 换行符 ( <br />
) 字符。
此外,由于您没有 html 换行符,因此您必须使用linebreaksbr
Django 模板过滤器将文本文件换行符转换为 HTML 换行符,如this answer中所建议的那样。
您的代码应如下所示:
{% autoescape on %}
{{ contact.info | linebreaksbr }}
{% endautoescape %}
你想要linebreaks
过滤器:
{{contact.info | linebreaks}}
话虽如此,如果contact.info
是一个列表会更好 - 然后你可以这样做:
{% for info in contact.info %}
{{ info }}<br />
{% endfor %}