Django Template Filters Reference

Django template filters are powerful tools for modifying data before rendering it in templates. Filters can transform variables, format data, and apply logic. This reference guide by The Coding College provides a comprehensive list of Django’s built-in filters, along with examples to help you use them effectively.

1. String Filters

capfirst

Capitalizes the first letter of the string.
Example:

{{ "hello world" | capfirst }}
<!-- Output: Hello world -->

center

Centers the string within a specified width, padding with spaces.
Example:

{{ "Django" | center:10 }}
<!-- Output: "   Django   " -->

cut

Removes all instances of a specified substring.
Example:

{{ "hello world" | cut:"o" }}
<!-- Output: hell wrld -->

title

Converts the string to title case (capitalizing each word).
Example:

{{ "django tutorial" | title }}
<!-- Output: Django Tutorial -->

truncatechars

Truncates a string after a specified number of characters, appending ....
Example:

{{ "This is a long string" | truncatechars:10 }}
<!-- Output: This is a... -->

truncatewords

Truncates a string after a specified number of words.
Example:

{{ "This is a long string" | truncatewords:3 }}
<!-- Output: This is a... -->

2. Date and Time Filters

date

Formats a date according to the given format string.
Example:

{{ my_date | date:"F j, Y" }}
<!-- Output: December 21, 2024 -->

time

Formats a time according to the given format string.
Example:

{{ my_time | time:"H:i" }}
<!-- Output: 14:35 -->

timesince

Returns the time elapsed since a date.
Example:

{{ my_date | timesince }}
<!-- Output: 3 days ago -->

timeuntil

Returns the time remaining until a date.
Example:

{{ my_date | timeuntil }}
<!-- Output: 2 days, 4 hours -->

3. Numeric Filters

add

Adds a value to the variable.
Example:

{{ 10 | add:5 }}
<!-- Output: 15 -->

divisibleby

Checks if the variable is divisible by a given value. Returns True or False.
Example:

{{ 10 | divisibleby:2 }}
<!-- Output: True -->

length

Returns the length of a string, list, or queryset.
Example:

{{ "Django" | length }}
<!-- Output: 6 -->

slice

Slices a list or string.
Example:

{{ "Django" | slice:":3" }}
<!-- Output: Dja -->

4. Boolean Filters

yesno

Converts True, False, or None to a custom output.
Example:

{{ my_var | yesno:"Yes,No,Maybe" }}
<!-- Output: Yes (if True), No (if False), Maybe (if None) -->

5. List and Dict Filters

dictsort

Sorts a list of dictionaries by a specified key.
Example:

{{ my_list | dictsort:"name" }}

dictsortreversed

Sorts a list of dictionaries by a specified key in reverse order.
Example:

{{ my_list | dictsortreversed:"name" }}

first

Returns the first item in a list.
Example:

{{ my_list | first }}

join

Joins a list into a single string with a specified delimiter.
Example:

{{ my_list | join:", " }}
<!-- Output: item1, item2, item3 -->

last

Returns the last item in a list.
Example:

{{ my_list | last }}

6. Conditional Filters

default

Returns a default value if the variable is empty or undefined.
Example:

{{ my_var | default:"Default Value" }}

default_if_none

Returns a default value if the variable is None.
Example:

{{ my_var | default_if_none:"Default Value" }}

7. Escaping and Formatting Filters

escape

Escapes HTML characters to prevent XSS attacks.
Example:

{{ "<b>bold</b>" | escape }}
<!-- Output: <b>bold</b> -->

safe

Marks a string as safe, allowing it to render as raw HTML.
Example:

{{ "<b>bold</b>" | safe }}
<!-- Output: <b>bold</b> -->

floatformat

Formats a number to a fixed number of decimal places.
Example:

{{ 123.456 | floatformat:2 }}
<!-- Output: 123.46 -->

Final Thoughts

Django filters are essential for creating dynamic, flexible templates. By mastering these filters, you can effectively manage data presentation and simplify template logic.

For more tutorials and resources, visit The Coding College!

Leave a Comment