Django 文本字段中的新行

发布于 2025-01-13 11:54:26 字数 459 浏览 1 评论 0原文

在我的 Django 应用程序中,我有:

models.py

content_don = models.TextField()

我将内容写入该字段,然后使用“提交”保存。问题是,在我的页面上我预览它:

post.html

<p class="article-content mt-3 mb-1"><strong>Description: </strong></p>
<div class="media content-section mt-1  mb-1">
    <p class="lead">{{ post.content_don }}</p>
</div>

所有内容都在同一行中。现在,我在表单输入期间写入文本时输入了一些行。这是为什么?

In my Django app I have:

models.py

content_don = models.TextField()

I write content into that field and then save it with 'Submit'. Problem is, on my page I preview it with:

post.html

<p class="article-content mt-3 mb-1"><strong>Description: </strong></p>
<div class="media content-section mt-1  mb-1">
    <p class="lead">{{ post.content_don }}</p>
</div>

All content is in the same row. There are now rows which I typed in when I wrote text during form input. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2025-01-20 11:54:26

HTML 需要将换行符转换为标记,通常是
。有一个 Django 内置 linebreaks 可以做到这一点。

{{ post.content_don|linebreaks }}

HTML needs the newlines converted into markup, typically <br>. There's a Django builtin linebreaks to do this.

{{ post.content_don|linebreaks }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文