首页 > 技术文章 > django textfield 换行问题

littlefire 2014-04-09 22:16 原文

用的是linux,

首先把model中的代码改成:

1 def save(self):
2         self.body = self.body.replace("\r\n", "\n")
3         self.body = self.body.replace("\n","</br>|safe")
4         super(Diary, self).save()
5     

当然不知道为什么还是没用。。"<" 和 ">" 还是会被转义

两个群都没有人回答问题啊好凄惨。

先不管这个,把add diary 那个页面做好再说。

完事儿再查。

 

---------------------------update-------------------------------

好丢人,,模版内部内置了filter的。

linebreaks

Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (<br />) and a new line followed by a blank line becomes a paragraph break (</p>).

For example:

{{ value|linebreaks }}

If value is Joel\nis a slug, the output will be <p>Joel<br />is a slug</p>.

一个filter就搞定了。。要多读文档哦

推荐阅读