Html Django:允许从 textarea 输入换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1894930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django: allow line break from textarea input
提问by barin
How do I allow line breaking in textarea input in django to later show this input on page?
如何允许在 django 中的 textarea 输入中换行以稍后在页面上显示此输入?
回答by barin
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>
).
用适当的 HTML 替换纯文本中的换行符;单个换行符成为 HTML 换行符 ( <br />
),新行后跟一个空行成为分段符( </p>
)。
For example:
例如:
{{ value|linebreaks }}
If value is Joel\nis a slug
, the output will be <p>Joel<br />is a slug</p>.
如果值为Joel\nis a slug
,则输出将为<p>Joel<br />is a slug</p>.
回答by barin
Don't use {% autoescape off %}
! Otherwise user controlled input may not get escaped, which is a security risk. As mentioned use linebreaks or linebreaksbr.
不要用{% autoescape off %}
!否则用户控制的输入可能不会被转义,这是一个安全风险。如前所述,使用linebreaks 或 linebreaksbr。
回答by Emanuel Faisca
I had a text area for user minimal custom input in the template and I wanted to keep whatever formatted text the user inserted to stay the same. The solution, I simply changed the Model variable to a TextField type. It even shows the user formatted text in the admin. :)
我在模板中有一个用于用户最小自定义输入的文本区域,我想保留用户插入的任何格式化文本以保持不变。解决方案,我只是将 Model 变量更改为 TextField 类型。它甚至在管理员中显示用户格式化的文本。:)
ex.
前任。
class Uadds(models.Model):
title = models.CharField(max_length = 50)
description = models.TextField(max_length = 1000)
title will not show line breaks, however, description will show them. I haven't tested this with a Rich Text Editor... Hope this helped.
title 不会显示换行符,但是 description 会显示它们。我还没有用富文本编辑器测试过这个......希望这会有所帮助。
回答by Haridas
Use {% autoescape off %} {{ your_variable }} {% endautoescape %}
.
使用{% autoescape off %} {{ your_variable }} {% endautoescape %}
.