Limiting the content of TextField in django templates

I am trying to create a blog index page where all the blog post entries are shown. But i want to limit the content of the post body to certain amount (Similar to any blog you see on the internet) so not all the content is visible but when user click on Read More link he can see that particular post in details.

I know how to create page for single article but i am not able to figure out how to limit the post body content. Do i need to change anything in the model or i can do this directly from templates

<h1><a href="/blog/{{ post.slug }}/">{{ post.title }}</a></h1>
<p>{{ post.post_body }}</p>
<a href="/blog/{{ post.slug }}/" class="btn btn-default">Read More</a>  

I have declared post body as textfield

post_body = models.TextField()

Answer

truncatechars¶

Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence (”…”).

Argument: Number of characters to truncate to

For example:

{{ value|truncatechars:9 }} If value is “Joel is a slug”, the output will be “Joel i…”.

docs

Leave a Reply

Your email address will not be published. Required fields are marked *