Built-In Filters In Django Templates

Feb. 8, 2023, 10:50 a.m.

django python
Django Template Engine provides filters which are used to transform the values of variables and tag arguments.Tags can’t modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one’s own need.
Syntax
{{ variable_name | filter_name }}

We are going to discuss about following Filters:

  • add
  • capfirst
  • center
  • cut
  • default
  • dictsort
  • divisibleby
  • escape
  • join
  • last
  • length
  • linenumbers
  • lower
  • make_list
  • random
  • slice
  • slugify
  • title
  • upper
  • wordcount

1. add

It is used to add an argument to value.
{{ value | add:"2" }}
if the value is 4, then the output will be 6. This Filter can be used to increament a variable in django Template.

2. capfirst

This filter is used to capitalize the first character of the value. Note : If the first character is not a letter, this filter has no effect.
{{ value | capfirst }}
if the value is "django", then the output will be "Django".

3. center

It is used to center the value in a field of a given width.
{{ value | center:15" }}
if the value is 4, then the output will be 6. This Filter can be used to increament a variable in django Template.

4. cut

It is used to remove all the value of arg from the given string.
{{ value | cut:" " }}
if the value is "Django is awesome ", then the output will be "Djangoisawesome". 

5. default

It is used to a default value to a variable. if variable evaluates to False, it will use the default argument given else the variable value itself;
{{ value | default:"default value" }}
if the value is "", then the output will be "default value".

6. dictsort

It takes a list of dictionaries and returns that list sorted by the key given in the argument.
{{ value | dictsort:"name" }}
if the value is :
[
    {'name': 'zed', 'age': 19},
    {'name': 'amy', 'age': 22},
    {'name': 'joe', 'age': 31},
]
 then the output will be :
[
    {'name': 'amy', 'age': 22},
    {'name': 'joe', 'age': 31},
    {'name': 'zed', 'age': 19},
] 

7. divisibleby

It returns true if the value is divisible by the argument.
{{ value | divisibleby:"3" }}
if the value is 21, then the output would be True.

8. escape

It is used to escape a string's HTML. Specifically, it makes these replacements.
{{ value | escape }}

9. first

It is used to return the first item in a list.
{{ value | first }}
If value is the list [‘a’, ‘b’, ‘c’], the output will be ‘a’.

10. join

It is used to join a string, like Python's str.join(list).
{{ value | join:"//" }}
If value is the list [‘a’, ‘b’, ‘c’], the output will be the string “a // b // c”.
 

11. last

It is used to return the last item in a list.
{{ value | last }}
If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output will be the string “d”.

12. length

It is used to return the length of the value. This works for both lists and strings
{{ value | length }}
If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output will be 4.

13. linenumbers

It is used to display text with line numbers.
{{ value | linenumbers }}
if the value is:
one
two
three

then the output will be:

1. one
2. two
3. three

14. lower

It is used to converts a string into all lowercase
{{ value | lower }}
If value is Learn With Me , the output will be learn with me.

15. make_list

It is used to return the value turned into a list. For strings it's a string of characters. For an integer, the argument is cast to a string before creating a list.
{{ value | make_list }}
If value is the string “Hello”, the output would be the list [‘H’, ‘e’, ‘l’, ‘l’, ‘o’]. If value is 123, the output will be the list [‘1’, ‘2’, ‘3’].

16. random

It is used to return a random item from the given list.
{{ value | random }}
If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output could be “c”.

17. slice

It is used to return a slice of the list
{{ some_list | slice:":2" }}
If some_list is [‘a’, ‘b’, ‘c’], the output will be [‘a’, ‘b’].

18. slugify

It is used to It is used to convert to ASCII. It converts spaces to hyphens and removes characters that aren’t alphanumerics, underscores, or hyphens. Converts to lowercase. Also strips leading and trailing whitespace.
{{ value | slugify }}
 
If value is “hello learner ”, the output will be “hello-learner”.
 

19. title

It is used to convert a string into titlecase by making words start with an uppercase character and the remaining characters lowercase. This filter makes no effort to keep “trivial words” in lowercase.
{{ value | title }}
If value is “different filters in django”, the output will be “Different Filters In Django”.

20. upper

It is used to convert a string into all uppercase.
{{ value | uppercase }}
If value is “hello learner”, the output will be “HELLO LEARNER”.

21. wordcount

It is used to return the numbers of words.
{{ value | wordcount }}
If value is “different filters in django”, the output will be 4.
author image

bracketcoders

A learing portal for your coding interest.

View Profile