Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show first 50 words of a text field in django template

Tags:

I have a field like this in my Django template:

<p>{{news.description}}<p>

I wanna show the first 50 words of this field. How can I do it?

like image 481
Asma Gheisari Avatar asked Oct 19 '11 19:10

Asma Gheisari


People also ask

How do I make the first letter capital in Django?

Use {{ obj | capfirst }} to make uppercase the first character only. Using {{ obj | title }} makes it Camel Case.

How do I create a text field in Django?

For example, adding an argument null = True to TextField will enable it to store empty values for that table in relational database. Here are the field options and attributes that an TextField can use. If True, Django will store empty values as NULL in the database. Default is False.

What does {% include %} means in Django?

{% include %}: this will insert a template within the current one. Be aware that the included template will receive the request's context, and you can give it custom variables too.

What does the built in Django template tag Lorem do?

lorem. Displays random “lorem ipsum” Latin text. This is useful for providing sample data in templates.


1 Answers

From the documentation:

{{ news.description|truncatewords:50 }}
like image 186
Alison R. Avatar answered Oct 13 '22 03:10

Alison R.