(django问题)处理换行和空格

来源:互联网 发布:冬天饮品 知乎 编辑:程序博客网 时间:2024/05/18 11:32

后台传递给template在页面显示时不能实现换行和空格

解决方法:

方案一:使用 HTML pre tag

<div class="content">        <div class="title">            <h3>{{ article.title }}</h3>            <a href="{% url 'blog:add_page' article.id %}">修改文章</a>        </div>        <pre>{{ article.content }}</pre>    </div>

缺点是得自己控制长度,不能在admin中控制长度

方案二:使用 linebreaks filter

<div class="content">        <div class="title">            <h3>{{ article.title }}</h3>            <a href="{% url 'blog:add_page' article.id %}">修改文章</a>        </div>        <p>{{ article.content|linebreaksbr }}</p>    </div>

缺点是不能处理多个连续空格。可以自己写一个 filter 把所有的空格转为,解决方法 http://stackoverflow.com/questions/721035/django-templates-stripping-spaces

0 0