如何在不使用所见即所得编辑器的情况下在 Django 帖子中保留新行?

How to preseve newline in django posts without using a WYSIWYG editor?

我正在使用 django 的表单 class 及其 cleaned_data 方法来获取用户 posts/comments。问题是 cleaned_data 字段中没有保留换行符,所以

The firs line of comment.
And the second lines.

最终成为

The firs line of comment. And the second lines.

哪个不好

如何在不使用所见即所得编辑器的情况下避免这种情况?

如果您指的是字段中保存的值的输出,您只需要使用 linebreakslinebreaksbr 过滤器:https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#linebreaks

{{ instance.field|linebreaks }}

{{ instance.field|linebreaksbr }}

无需使用所见即所得的编辑器即可获得正确的格式。