我是django的新手。我有一个纯文本,包含许多段落,在django admin中输入。 这是从互联网上复制的痛苦文字
示例输入
A mysterious landscape phenomenon known as fairy circles has been found in the Australian outback. The fairy circles are characterised by a hexagonal organisation of soil gaps between grass vegetation and seen in the landscape from above.
The beautiful sight cannot be spotted from ground level. Until now, fairy circles have only been documented in the arid landscape of Namibia, Africa.
示例输出:
A mysterious landscape phenomenon known as fairy circles has been found in the Australian outback. The fairy circles are characterised by a hexagonal organisation of soil gaps between grass vegetation and seen in the landscape from above.
The beautiful sight cannot be spotted from ground level. Until now, fairy circles have only been documented in the arid landscape of Namibia, Africa.
当我使用 {{ posts.description|linebreaks }}
它只是给了我一个换行符(一个换行符)。在我的chrome控制台中,它应该是一个<br />
但我希望 2次换行
我尝试使用{{ posts.description|linebreaks|linebreaks }}
,但没有帮助
我应该如何插入2个换行符(两个换行符)?
感谢任何帮助。谢谢提前
答案 0 :(得分:1)
您可以根据换行符编写自己的自定义标记(甚至可以复制某些功能)。这很容易,但实际上不鼓励 - 这就是它在标准django模板过滤器中不存在的原因。
但除此之外,您甚至可以根据需要拆分字符串并进行渲染:
例如,您可以按照here所述获得拆分描述。然后按如下方式呈现描述:
{% for line in posts.descirption_as_list %}
{{ line }}<br/><br/>
{% endfor %}
如果你真的想走模板标签过滤器的路线:
@register.filter(name="split_by")
def split_by(value, split_by='\n'):
return value.split(split_by)
并将其用作
{% for line in posts.descirption|split_by:"\n" %}
{{ line }}<br/><br/>
{% endfor %}
在上面你甚至不需要指定第二个参数(“\ n”)。也就是说,你可以使用它如下:
{% for line in posts.descirption|split_by %}
{{ line }}<br/><br/>
{% endfor %}
但是现在这不太可读,不是吗?明智地选择。