Я создал сайт вопросов и ответов в Django. Я хочу печатать текст в том же формате, в котором он был введен.
This is the text
This is on a new line
Он должен печатать в этом формате по мере ввода, но печатает вот так
This is the text This is on a new line
мой код forum.html
<div class="card-body cardbody">
<p>{{post.post_content|truncatewords:"10"|linebreaks}}</p>{% if post.post_content|length|get_digit:"-1" > 50 %}
<a href="/discussion/{{post.id}}" data-abc="true"><button class="btn btn-light" style="color:blue; font-size: 13px;">Show more </button> </a>
{% endif %}
</div>
пожалуйста, помогите мне сделать это
В документации для обрезать слова упоминается:
Newlines within the string will be removed.
Следовательно, вам нужно сначала использовать фильтр linebreaks
, а затем вместо truncatewords
вы должны использовать truncatewords_html
:
{{ post.post_content|linebreaks|truncatewords_html:10 }}