Shortening HTML content python / django -


i have system pulls html content database this:

<p>this text</p> <p>more text</p> <p>third paragraph</p> 

and when displaying it, displays short summary of blog post content right when displaying text, have set limit of characters number 200 , cause summary this:

<p>this text</p> <p>more text</ 

cutting off content @ place, potentially destroying rest of webpage. there way in python either close opened tags or number of top-level blocks? instance, 2 blocks first 2 <p></p> tags. have seen word press have feature when displaying short previews of articles.

if need in template take @ truncatewords-html: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#truncatewords-html

or elsewhere can use underlying django.utils.text.truncator class (https://github.com/django/django/blob/1.7/django/utils/text.py#l65) should work use cases, if need custom behavior use code reference implement yourself.

update:

if need use django.utils.text.truncator directly, how instantiate , call words() method (this how truncatewords-html filter does)

truncator(value).words(length, html=true, truncate=' ...') 

Comments