Adds the argument to the value. For example:
{{ value|add:"2" }}
If value
is 4
, then the output will be 6
.
Adds slashes before quotes. Useful for escaping strings in CSV, for example. For example:
{{ value|addslashes }}
If value
is I'm using Django
, the output will be I'm using Django
.
Capitalizes the first character of the value. If the first character is not a letter, this filter has no effect.
Centers the value in a field of a given width. For example:
"{{ value|center:"14" }}"
If value
is Django
, the output will be Django
.
Formats a date according to the given format. Uses a similar format as PHP's date()
function with some differences.
These format characters are not used in Django outside of templates. They were designed to be compatible with PHP to ease transitioning for designers. For a full list of format strings see the Django Project website at https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date.
For example:
{{ value|date:"D d M Y" }}
If value
is a datetime
object (for example, the result of datetime.datetime.now()
), the output will be the string Fri 01 Jul 2016
. The format passed can be one of the predefined ones DATE_FORMAT
, DATETIME_FORMAT
, SHORT_DATE_FORMAT
, or SHORT_DATETIME_FORMAT
, or a custom format that uses date format specifiers.
If value evaluates to False
, uses the given default. Otherwise, uses the value. For example:
{{ value|default:"nothing" }}
Takes a list of dictionaries and returns that list sorted by the key given in the argument. For example:
{{ value|dictsort:"name" }}
Takes a list of dictionaries and returns that list sorted in reverse order by the key given in the argument.
Returns True
if the value is divisible by the argument. For example:
{{ value|divisibleby:"3" }}
If value
is 21
, the output would be True
.
Escapes 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 &
The escaping is only applied when the string is output, so it does not matter where in a chained sequence of filters you put escape
: it will always be applied as though it were the last filter.
Escapes characters for use in JavaScript strings. This does not make the string safe for use in HTML, but does protect you from syntax errors when using templates to generate JavaScript/JSON.
Formats the value like a 'human-readable' file size (that is, '13 KB'
, '4.1 MB'
, '102 bytes'
, and more). For example:
{{ value|filesizeformat }}
If value
is 123456789
, the output would be 117.7 MB
.
When used without an argument, rounds a floating-point number to one decimal place-but only if there's a decimal part to be displayed. If used with a numeric integer argument, floatformat
rounds a number to that many decimal places.
For example, if value
is 34.23234
, {{ value|floatformat:3 }}
will output 34.232
.
Converts an Internationalized Resource Identifier (IRI) to a string that is suitable for including in a URL.
Returns True
if the value's length is the argument, or False
otherwise. For example:
{{ value|length_is:"4" }}
Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (<br />
) and a new line followed by a blank line becomes a paragraph break (</p>
).
Left-aligns the value in a field of a given width. For example:
{{ value|ljust:"10" }}
If value
is Django
, the output will be Django
.
Returns the value turned into a list. For a string, it's a list of characters. For an integer, the argument is cast into an Unicode string before creating a list.
Converts a phone number (possibly containing letters) to its numerical equivalent. The input doesn't have to be a valid phone number. This will happily convert any string. For example:
{{ value|phone2numeric }}
If value
is 800-COLLECT
, the output will be 800-2655328
.
Returns a plural suffix if the value is not 1
. By default, this suffix is s
.
For words that don't pluralize by simple suffix, you can specify both a singular and plural suffix, separated by a comma. Example:
You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.
Right-aligns the value in a field of a given width. For example:
{{ value|rjust:"10" }}
If value
is Django
, the output will be Django
.
Marks a string as not requiring further HTML escaping prior to output. When autoescaping is off, this filter has no effect.
Applies the safe
filter to each element of a sequence. Useful in conjunction with other filters that operate on sequences, such as join
. For example:
{{ some_list|safeseq|join:", " }}
You couldn't use the safe
filter directly in this case, as it would first convert the variable into a string, rather than working with the individual elements of the sequence.
Converts to ASCII. Converts spaces to hyphens. Removes characters that aren't alphanumeric, underscores, or hyphens. Converts to lowercase. Also strips leading and trailing whitespace.
Formats the variable according to the argument, a string formatting specifier. This specifier uses Python string formatting syntax, with the exception that the leading % is dropped.
Formats a time according to the given format. Given format can be the predefined one TIME_FORMAT
, or a custom format, same as the date
filter.
Formats a date as the time since that date (for example, 4 days, 6 hours). Takes an optional argument that is a variable containing the date to use as the comparison point (without the argument, the comparison point is now
).
Converts a string into title case by making words start with an uppercase character and the remaining characters lowercase.
Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence (...). For example:
{{ value|truncatechars:9 }}
Recursively takes a self-nested list and returns an HTML unordered list-without opening and closing tags.
Converts URLs and email addresses in text into clickable links. This template tag works on links prefixed with http://
, https://
, or www.
.
Converts URLs and email addresses into clickable links just like urlize
, but truncates URLs longer than the given character limit. For example:
{{ value|urlizetrunc:15 }}
If value
is Check out www.djangoproject.com
, the output would be Check out <a href="http://www.djangoproject.com" rel="nofollow">www.djangopr...</a>
. As with urlize
, this filter should only be applied to plain text.