Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django两个新行字符_Python_Django_Django Filter - Fatal编程技术网

Python django两个新行字符

Python django两个新行字符,python,django,django-filter,Python,Django,Django Filter,我是django的新手。我有一个纯文本,其中有许多段落是在django admin中输入的这是从internet复制的疼痛文本 示例输入 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 betwe

我是django的新手。我有一个纯文本,其中有许多段落是在django admin中输入的这是从internet复制的疼痛文本

示例输入

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控制台中,它应该是一个

但我想有两次换行

我尝试使用
{{posts.description | linebreaks | linebreaks}}
,但没有帮助

如何插入两个换行符(两个新行字符)


非常感谢您的帮助。请提前感谢

您可以编写与换行符类似的自定义标记(甚至可以复制一些功能)。这很简单,但实际上并不鼓励使用它,这就是为什么标准django模板过滤器中不使用它的原因

但除此之外,您甚至可以拆分字符串并按照自己的意愿进行渲染:

例如,您可以获得如上所述的拆分描述。然后按如下方式呈现描述:

{% for line in posts.descirption_as_list %}
{{ line }}<br/><br/>
{% endfor %}
并将其用作

{% for line in posts.descirption|split_by:"\n" %}
{{ line }}<br/><br/>
{% endfor %}
{%用于POST中的行。描述选项|拆分为:“\n”%}
{{line}}

{%endfor%}
在上面的例子中,您甚至不需要指定第二个参数(“\n”)。也就是说,您可以按如下方式使用它:

{% for line in posts.descirption|split_by %}
{{ line }}<br/><br/>
{% endfor %}
{%用于帖子中的行。描述|按%|拆分}
{{line}}

{%endfor%}

但现在这不是很容易理解,是吗?明智地选择。

您可以编写与换行符类似的自定义标记(甚至可以复制一些功能)。这很简单,但实际上并不鼓励使用它,这就是为什么标准django模板过滤器中不使用它的原因

但除此之外,您甚至可以拆分字符串并按照自己的意愿进行渲染:

例如,您可以获得如上所述的拆分描述。然后按如下方式呈现描述:

{% for line in posts.descirption_as_list %}
{{ line }}<br/><br/>
{% endfor %}
并将其用作

{% for line in posts.descirption|split_by:"\n" %}
{{ line }}<br/><br/>
{% endfor %}
{%用于POST中的行。描述选项|拆分为:“\n”%}
{{line}}

{%endfor%}
在上面的例子中,您甚至不需要指定第二个参数(“\n”)。也就是说,您可以按如下方式使用它:

{% for line in posts.descirption|split_by %}
{{ line }}<br/><br/>
{% endfor %}
{%用于帖子中的行。描述|按%|拆分}
{{line}}

{%endfor%}

但现在这不是很容易理解,是吗?明智地选择。

您可以在
帖子中直接保存2个

。说明
(例如,替换
\n
)并在模板中应用
safe
过滤器感谢您的回复..请您提供一个示例以便易于理解…您可以直接在
帖子中保存2

。说明
(例如,替换
\n
)并在模板中应用
safe
过滤器感谢您的回复..请您提供一个示例,以便易于理解..@Coeus自定义过滤器会有所帮助,但不需要如前所述。除非你想做一些真正的事情,否则花哨的过滤器有点负担过重:对开发者来说(我想)。为什么要对实例本身可以完成的事情使用过滤器标记?一个很好的理由可能是:“我想在其他地方(甚至任何地方)重用它”。如果是这样的话,一定要去做。@Coeus一个定制的过滤器会有帮助,但不是像前面提到的那样必要。除非你想做一些真正的事情,否则花哨的过滤器有点负担过重:对开发者来说(我想)。为什么要对实例本身可以完成的事情使用过滤器标记?一个很好的理由可能是:“我想在其他地方(甚至任何地方)重用它”。如果是这样的话,一定要努力。