Django Template Filters - GeeksforGeeks

07 Feb, 2020
Django Template Engine provides filters which are used to transform the values of variables;es and tag arguments. We have already discussed major Django Template Tags. 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.
{{ variable_name | filter_name }}
Filters can be “chained.” The output of one filter is applied to the next. {{ text|escape|linebreaks }} is a common idiom for escaping text contents, then converting line breaks to <p> tags.
{{ value | length }}
If value is [‘a’, ‘b’, ‘c’, ‘d’], the output will be 4.
This article revolves around various Django Template Filters one can use during a project. Filters transform the values of variables and tag arguments. Let’s check some major filters.
addIt is used to add an argument to the value.Example{{ value | add:"2" }}If value is 4, then the output will be 6. This filter is used to increment a variable in django Templates.addslashesIt is used to add slashes before quotes. Useful for escaping strings in CSV.Example{{ value | addslashes }}If value is “I’m Jai”, the output will be “I\’m Jai”.capfirstIt is used to capitalize the first character of the value. If the first character is not a letter, this filter has no effect.Example{{ value | capfirst }}If value is “jai”, the output will be “Jai”.centerIt is used to center the value in a field of a given width.Example"{{ value | center:"15" }}"If value is “Jai”, the output will be ” Jai “.cutIt is used to remove all values of arg from the given string.Example{{ value | cut:" " }}If value is “String with spaces”, the output will be “Stringwithspaces”.dateIt is used to format a date according to the given format.Example{{ value | date:"D d M Y" }}If value is a datetime object (e.g., the result of datetime.datetime.now()), the output will be the string ‘Thu 06 Feb 2020’. For More information and patterns visit here.defaultIt is used to to give a default value to a variable. If variable evaluates to False, it will use the default argument given else the variable value itself.Example{{ value | default:"nothing" }}If value is “” (the empty string), the output will be nothing.dictsortIt takes a list of dictionaries and returns that list sorted by the key given in the argument.Example{{ value | dictsort:"name" }}If value is:[
    {'name': 'zed', 'age': 19},
    {'name': 'amy', 'age': 22},
    {'name': 'joe', 'age': 31},
]then the output would be:[
    {'name': 'amy', 'age': 22},
    {'name': 'joe', 'age': 31},
    {'name': 'zed', 'age': 19},
] divisiblebyIt returns True if the value is divisible by the argument.Example{{ value | divisibleby:"3" }}If value is 21, the output would be True.escapeIt is used to escapea a string’s HTML. Specifically, it makes these replacements:< is converted to <> is converted to >' (single quote) is converted to '" (double quote) is converted to "& is converted to &Example{{ title | escape }}filesizeformatIt is used to format the value like a ‘human-readable’ file size (i.e. ’13 KB’, ‘4.1 MB’, ‘102 bytes’, etc.).Example{{ value | filesizeformat }}If value is 123456789, the output would be 117.7 MB.firstIt is used to return the first item in a list.Example{{ value | first }}If value is the list [‘a’, ‘b’, ‘c’], the output will be ‘a’.joinIt is used to join a list with a string, like Python’s str.join(list)Example{{ value | join:" // " }}If value is the list [‘a’, ‘b’, ‘c’], the output will be the string “a // b // c”.lastIt is used to return the last item in a list.Example{{ value | last }}If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output will be the string “d”.lengthIt is used to return the length of the value. This works for both strings and lists.Example{{ value | length }}If value is [‘a’, ‘b’, ‘c’, ‘d’] or “abcd”, the output will be 4.linenumbersIt is used to display text with line numbers.Example{{ value | linenumbers }}If value is:one
two
threethe output will be:1. one
2. two
3. threelowerIt is used to converts a string into all lowercase.Example{{ value | lower }}}If value is My Name is Jai, the output will be my name is jai.make_listIt is used to return the value turned into a list. For a string, it’s a list of characters. For an integer, the argument is cast to a string before creating a list.Example{{ value | make_list }}If value is the string “Naveen”, the output would be the list [‘N’, ‘a’, ‘v’, ‘e’, ‘e’, ‘n’]. If value is 123, the output will be the list [‘1’, ‘2’, ‘3’].randomIt is used to return a random item from the given list.Example{{ value | random }}If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output could be “b”.sliceIt is used to return a slice of the list.Example{{ some_list | slice:":2" }}If some_list is [‘a’, ‘b’, ‘c’], the output will be [‘a’, ‘b’].slugifyIt 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.Example{{ value | slugify }}If value is “Jai is a slug”, the output will be “jai-is-a-slug”.timeIt is used to format a time according to the given format.Example{{ value | time:"H:i" }}If value is equivalent to datetime.datetime.now(), the output will be the string “01:23”.timesinceIt is used to format a date as the time since that date (e.g., “4 days, 6 hours”).Example{{ blog_date | timesince:comment_date }}if blog_date is a date instance representing midnight on 1 June 2006, and comment_date is a date instance for 08:00 on 1 June 2006, then the following would return “8 hours”titleIt 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.Example{{ value | title }}If value is “my FIRST post”, the output will be “My First Post”.unordered_listIt is used to recursively take a self-nested list and returns an HTML unordered list – WITHOUT opening and closing <ul> tags.Example{{ var | unordered_list }}if var contains ['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']], then {{ var|unordered_list }} would return:<li>States<ul>        <li>Kansas        <ul>                <li>Lawrence</li>                <li>Topeka</li>        </ul>        </li>        <li>Illinois</li></ul></li>         </li>upperIt is used to convert a string into all uppercase.Example{{ value | upper }}If value is “Jai is a slug”, the output will be “JAI IS A SLUG”.wordcountIt is used to return the number of words.Example{{ value | wordcount }}If value is “jai is a slug”, the output will be 4.
addIt is used to add an argument to the value.Example{{ value | add:"2" }}If value is 4, then the output will be 6. This filter is used to increment a variable in django Templates.
It is used to add an argument to the value.Example
{{ value | add:"2" }}
If value is 4, then the output will be 6. This filter is used to increment a variable in django Templates.
addslashesIt is used to add slashes before quotes. Useful for escaping strings in CSV.Example{{ value | addslashes }}If value is “I’m Jai”, the output will be “I\’m Jai”.
It is used to add slashes before quotes. Useful for escaping strings in CSV.Example
{{ value | addslashes }}
If value is “I’m Jai”, the output will be “I\’m Jai”.
capfirstIt is used to capitalize the first character of the value. If the first character is not a letter, this filter has no effect.Example{{ value | capfirst }}If value is “jai”, the output will be “Jai”.
It is used to capitalize the first character of the value. If the first character is not a letter, this filter has no effect.Example
{{ value | capfirst }}
If value is “jai”, the output will be “Jai”.
centerIt is used to center the value in a field of a given width.Example"{{ value | center:"15" }}"If value is “Jai”, the output will be ” Jai “.
It is used to center the value in a field of a given width.Example
"{{ value | center:"15" }}"
If value is “Jai”, the output will be ” Jai “.
cutIt is used to remove all values of arg from the given string.Example{{ value | cut:" " }}If value is “String with spaces”, the output will be “Stringwithspaces”.
It is used to remove all values of arg from the given string.
Example
{{ value | cut:" " }}
If value is “String with spaces”, the output will be “Stringwithspaces”.
dateIt is used to format a date according to the given format.Example{{ value | date:"D d M Y" }}If value is a datetime object (e.g., the result of datetime.datetime.now()), the output will be the string ‘Thu 06 Feb 2020’. For More information and patterns visit here.
It is used to format a date according to the given format.Example
{{ value | date:"D d M Y" }}
If value is a datetime object (e.g., the result of datetime.datetime.now()), the output will be the string ‘Thu 06 Feb 2020’. For More information and patterns visit here.
defaultIt is used to to give a default value to a variable. If variable evaluates to False, it will use the default argument given else the variable value itself.Example{{ value | default:"nothing" }}If value is “” (the empty string), the output will be nothing.
It is used to to give a default value to a variable. If variable evaluates to False, it will use the default argument given else the variable value itself.
Example
{{ value | default:"nothing" }}
If value is “” (the empty string), the output will be nothing.
dictsortIt takes a list of dictionaries and returns that list sorted by the key given in the argument.Example{{ value | dictsort:"name" }}If value is:[
    {'name': 'zed', 'age': 19},
    {'name': 'amy', 'age': 22},
    {'name': 'joe', 'age': 31},
]then the output would be:[
    {'name': 'amy', 'age': 22},
    {'name': 'joe', 'age': 31},
    {'name': 'zed', 'age': 19},
] 
It takes a list of dictionaries and returns that list sorted by the key given in the argument.Example
{{ value | dictsort:"name" }}
If value is:
[
    {'name': 'zed', 'age': 19},
    {'name': 'amy', 'age': 22},
    {'name': 'joe', 'age': 31},
]
then the output would be:
[
    {'name': 'amy', 'age': 22},
    {'name': 'joe', 'age': 31},
    {'name': 'zed', 'age': 19},
] 
divisiblebyIt returns True if the value is divisible by the argument.Example{{ value | divisibleby:"3" }}If value is 21, the output would be True.
It returns True if the value is divisible by the argument.Example
{{ value | divisibleby:"3" }}
If value is 21, the output would be True.
escapeIt is used to escapea a string’s HTML. Specifically, it makes these replacements:< is converted to <> is converted to >' (single quote) is converted to '" (double quote) is converted to "& is converted to &Example{{ title | escape }}
It is used to escapea a string’s HTML. Specifically, it makes these replacements:
< is converted to <> is converted to >' (single quote) is converted to '" (double quote) is converted to "& is converted to &
Example
{{ title | escape }}
filesizeformatIt is used to format the value like a ‘human-readable’ file size (i.e. ’13 KB’, ‘4.1 MB’, ‘102 bytes’, etc.).Example{{ value | filesizeformat }}If value is 123456789, the output would be 117.7 MB.
It is used to format the value like a ‘human-readable’ file size (i.e. ’13 KB’, ‘4.1 MB’, ‘102 bytes’, etc.).Example
{{ value | filesizeformat }}
If value is 123456789, the output would be 117.7 MB.
firstIt is used to return the first item in a list.Example{{ value | first }}If value is the list [‘a’, ‘b’, ‘c’], the output will be ‘a’.
It is used to return the first item in a list.Example
{{ value | first }}
If value is the list [‘a’, ‘b’, ‘c’], the output will be ‘a’.
joinIt is used to join a list with a string, like Python’s str.join(list)Example{{ value | join:" // " }}If value is the list [‘a’, ‘b’, ‘c’], the output will be the string “a // b // c”.
It is used to join a list with a string, like Python’s str.join(list)Example
{{ value | join:" // " }}
If value is the list [‘a’, ‘b’, ‘c’], the output will be the string “a // b // c”.
lastIt is used to return the last item in a list.Example{{ value | last }}If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output will be the string “d”.
It is used to return the last item in a list.Example
{{ value | last }}
If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output will be the string “d”.
lengthIt is used to return the length of the value. This works for both strings and lists.Example{{ value | length }}If value is [‘a’, ‘b’, ‘c’, ‘d’] or “abcd”, the output will be 4.
It is used to return the length of the value. This works for both strings and lists.Example
{{ value | length }}
If value is [‘a’, ‘b’, ‘c’, ‘d’] or “abcd”, the output will be 4.
linenumbersIt is used to display text with line numbers.Example{{ value | linenumbers }}If value is:one
two
threethe output will be:1. one
2. two
3. three
It is used to display text with line numbers.Example
{{ value | linenumbers }}
If value is:
one
two
three
the output will be:
1. one
2. two
3. three
lowerIt is used to converts a string into all lowercase.Example{{ value | lower }}}If value is My Name is Jai, the output will be my name is jai.
It is used to converts a string into all lowercase.Example
{{ value | lower }}}
If value is My Name is Jai, the output will be my name is jai.
make_listIt is used to return the value turned into a list. For a string, it’s a list of characters. For an integer, the argument is cast to a string before creating a list.Example{{ value | make_list }}If value is the string “Naveen”, the output would be the list [‘N’, ‘a’, ‘v’, ‘e’, ‘e’, ‘n’]. If value is 123, the output will be the list [‘1’, ‘2’, ‘3’].
It is used to return the value turned into a list. For a string, it’s a list of characters. For an integer, the argument is cast to a string before creating a list.Example
{{ value | make_list }}
If value is the string “Naveen”, the output would be the list [‘N’, ‘a’, ‘v’, ‘e’, ‘e’, ‘n’]. If value is 123, the output will be the list [‘1’, ‘2’, ‘3’].
randomIt is used to return a random item from the given list.Example{{ value | random }}If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output could be “b”.
It is used to return a random item from the given list.Example
{{ value | random }}
If value is the list [‘a’, ‘b’, ‘c’, ‘d’], the output could be “b”.
sliceIt is used to return a slice of the list.Example{{ some_list | slice:":2" }}If some_list is [‘a’, ‘b’, ‘c’], the output will be [‘a’, ‘b’].
It is used to return a slice of the list.Example
{{ some_list | slice:":2" }}
If some_list is [‘a’, ‘b’, ‘c’], the output will be [‘a’, ‘b’].
slugifyIt 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.Example{{ value | slugify }}If value is “Jai is a slug”, the output will be “jai-is-a-slug”.
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.Example
{{ value | slugify }}
If value is “Jai is a slug”, the output will be “jai-is-a-slug”.
timeIt is used to format a time according to the given format.Example{{ value | time:"H:i" }}If value is equivalent to datetime.datetime.now(), the output will be the string “01:23”.
It is used to format a time according to the given format.Example
{{ value | time:"H:i" }}
If value is equivalent to datetime.datetime.now(), the output will be the string “01:23”.
timesinceIt is used to format a date as the time since that date (e.g., “4 days, 6 hours”).Example{{ blog_date | timesince:comment_date }}if blog_date is a date instance representing midnight on 1 June 2006, and comment_date is a date instance for 08:00 on 1 June 2006, then the following would return “8 hours”
It is used to format a date as the time since that date (e.g., “4 days, 6 hours”).
Example
{{ blog_date | timesince:comment_date }}
if blog_date is a date instance representing midnight on 1 June 2006, and comment_date is a date instance for 08:00 on 1 June 2006, then the following would return “8 hours”
titleIt 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.Example{{ value | title }}If value is “my FIRST post”, the output will be “My First Post”.
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.
Example
{{ value | title }}
If value is “my FIRST post”, the output will be “My First Post”.
unordered_listIt is used to recursively take a self-nested list and returns an HTML unordered list – WITHOUT opening and closing <ul> tags.Example{{ var | unordered_list }}if var contains ['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']], then {{ var|unordered_list }} would return:<li>States<ul>        <li>Kansas        <ul>                <li>Lawrence</li>                <li>Topeka</li>        </ul>        </li>        <li>Illinois</li></ul></li>         </li>
It is used to recursively take a self-nested list and returns an HTML unordered list – WITHOUT opening and closing <ul> tags.Example
{{ var | unordered_list }}
if var contains ['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']], then {{ var|unordered_list }} would return:
<li>States<ul>        <li>Kansas        <ul>                <li>Lawrence</li>                <li>Topeka</li>        </ul>        </li>        <li>Illinois</li></ul></li>         </li>
upperIt is used to convert a string into all uppercase.Example{{ value | upper }}If value is “Jai is a slug”, the output will be “JAI IS A SLUG”.
It is used to convert a string into all uppercase.Example
{{ value | upper }}
If value is “Jai is a slug”, the output will be “JAI IS A SLUG”.
wordcountIt is used to return the number of words.Example{{ value | wordcount }}If value is “jai is a slug”, the output will be 4.
It is used to return the number of words.Example
{{ value | wordcount }}
If value is “jai is a slug”, the output will be 4.
Django-templates
Python Django
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Python program to convert a list to string
Reading and Writing to text files in Python