DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for My Favorite Django πŸ¦„ Template Filters
Vijay Soni
Vijay Soni

Posted on

My Favorite Django πŸ¦„ Template Filters

πŸ– Hello Guys!
In this tutorial you will become familiar my favorite django template filters.

1. Timesince

If you want to show the date-time-field of a django model in human familiar term you can use this filter like this:

{{ field_date|timesince }}
Enter fullscreen mode Exit fullscreen mode

and it will return you the time like 2 days ago format

2. Linebreaks

If you want to show the text of a field of a django model as you write in the form (with spaces, and tabs) you could use this filter in template like this:

{{ value|linebreaks }}
Enter fullscreen mode Exit fullscreen mode

3. Truncatewords

If you want to display the first n words of a django model field in the template you can use this filter. For example if you want to show the first two words of the text you can use the filter like this:

{{ text|truncatewords:2 }}
Enter fullscreen mode Exit fullscreen mode

and the output will be like Hello Python ...

4. Urlize

If you want to make an url active of the text of a django model field you can use this template filter like this:

{{ text|urlize }}
Enter fullscreen mode Exit fullscreen mode

5. Wordcount

If you want to show the number of words of a django model field in the template you can use this filter like this:

{{ text|wordcount }}
Enter fullscreen mode Exit fullscreen mode

and it will return the number of the words in the template

6. Title

If you want to show the text of a django model field in Title Case you can use this template filter like this:

{{ text|title }}
Enter fullscreen mode Exit fullscreen mode

You can use |upper and |lower for respectively UPPER CASE and lower case.

for a detail knowledge of django template filters and tags you can visit djangotemplatetagsandfilters.com

πŸ‘Thank you for reading my post!

Top comments (6)

Collapse
 
dennisivy11 profile image
Dennis Ivy

Great article!

Collapse
 
vijaysoni007 profile image
Vijay Soni Author

Thank you Dennis, I'm also a big fan of you on youtube.

Collapse
 
user_8d5c77b226 profile image

Great

Collapse
 
asadbek profile image
Asadbek

Awesome!

Collapse
 
muhammadabir profile image
Muhammad ABir

thanks a lot brother..... even I didn't know all of this existedπŸ˜…

Collapse
 
vijaysoni007 profile image
Vijay Soni Author

You're welcome

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.