1

If I have a large body of text, e.g. a blog post, how could I display the entire first paragraph of each text on the blog's home page, instead of breaking it on a random letter in a random word with the following?

  <p>{{ post.text|linebreaksbr|slice:":400" }}</p>
Zaff
  • 137
  • 3
  • 15
  • 1
    Define a paragraph – DeepSpace Apr 09 '19 at 19:55
  • 1
    Looking through the built-in filters, the closest fit for this situation is [truncatewords](https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#truncatewords), which at least splits it at the end of a word. For more complex rules than that, you will have to define your own filter. – Robin Zigmond Apr 09 '19 at 19:59

1 Answers1

2

Try avoiding this type of operations in your templates, they are not designed for that. Instead, implement that functionality in your models.py or views.py with the appropriate split('\n')

Have a look at Django templates - split string to array.

caravana_942
  • 632
  • 1
  • 8
  • 26