django字符串在终端中显示良好但在模板中不显示

时间:2014-06-02 12:16:11

标签: python django string format

我正在制作一个消息系统,但在我发送回复之后,我的邮件的tempalte视图全部放在一行,而当我在终端打印相同的字符串时,它工作得很好。

模板:

<div>
To: {{ mail.to }}<br>
From: {{ mail.sender }}<br>
{{ mail.view_sent_at }}<br>
</div>

<div>
<br>
{{ mail.body }}
</div>

我的回复正文功能: (改变身体显示为重播)

def reply_format(self):
    message = self.body
    message = "> " + message
    message = message.replace("\r\n", "\r\n> ")
    send_information = "\r\n\r\n> To: %s\r\n> From: %s\r\n> Date: %s\r\n>\r\n" % (self.to, self.sender, self.view_sent_at())
    message = send_information + message
    return message

现在作为示例,当我保存它时,我的邮件正文看起来像这样(以及它在我的终端后的样子)。

Send a reply

> To: admin
> From: admin
> Date: 12:14 02-06-2014
>
> This is a test.
> Need some extra lines
> Byebye

当我在模板中打开它时,它看起来像:

To: admin
From: admin
12:14 02-06-2014

Send a reply > To: admin > From: admin > Date: 12:14 02-06-2014 > > This is a test. > Need some extra lines > Byebye 

1 个答案:

答案 0 :(得分:2)

您可以使用模板中的linebreaks filter将换行符转换为相应的HTML标记。