Module defaultfilters
Default variable filters.
source linkFunctions
▷ def add(value, arg) @register.filter(is_safe=False) Add the arg to the value.
@register.filter(is_safe=False)
def add(
value,
arg,
)
▷ def addslashes(value) @register.filter(is_safe=True) @stringfilter Add slashes before quotes. Useful for escaping strings in CSV, for example. Less useful for escaping JavaScript; use the ``escapejs`` filter instead.
@register.filter(is_safe=True)
@stringfilter
def addslashes(value)
▶ def capfirst(value) @register.filter(is_safe=True) @stringfilter Capitalize the first character of the value.
@register.filter(is_safe=True)
@stringfilter
def capfirst(value)
Reexports
▷ def center(value, arg) @register.filter(is_safe=True) @stringfilter Center the value in a field of a given width.
@register.filter(is_safe=True)
@stringfilter
def center(
value,
arg,
)
▷ def cut(value, arg) @register.filter @stringfilter Remove all values of arg from the given string.
@register.filter
@stringfilter
def cut(
value,
arg,
)
▶ def date(value, arg=None) @register.filter(expects_localtime=True, is_safe=False) Format a date according to the given format.
@register.filter(expects_localtime=True, is_safe=False)
def date(
value,
arg=None,
)
Reexports
▷ def default(value, arg) @register.filter(is_safe=False) If value is unavailable, use given default.
@register.filter(is_safe=False)
def default(
value,
arg,
)
▷ def default_if_none(value, arg) @register.filter(is_safe=False) If value is None, use given default.
@register.filter(is_safe=False)
def default_if_none(
value,
arg,
)
▷ def dictsort(value, arg) @register.filter(is_safe=False) Given a list of dicts, return that list sorted by the property given in the argument.
@register.filter(is_safe=False)
def dictsort(
value,
arg,
)
▷ def dictsortreversed(value, arg) @register.filter(is_safe=False) Given a list of dicts, return that list sorted in reverse order by the property given in the argument.
@register.filter(is_safe=False)
def dictsortreversed(
value,
arg,
)
▷ def divisibleby(value, arg) @register.filter(is_safe=False) Return True if the value is divisible by the argument.
@register.filter(is_safe=False)
def divisibleby(
value,
arg,
)
▷ def escape_filter(value) @register.filter("escape", is_safe=True) @stringfilter Mark the value as a string that should be auto-escaped.
@register.filter("escape", is_safe=True)
@stringfilter
def escape_filter(value)
▷ def escapejs_filter(value) @register.filter("escapejs") @stringfilter Hex encode characters for use in JavaScript strings.
@register.filter("escapejs")
@stringfilter
def escapejs_filter(value)
▷ def filesizeformat(bytes_) @register.filter(is_safe=True) Format the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB, 102 bytes, etc.).
▷ def first(value) @register.filter(is_safe=False) Return the first item in a list.
@register.filter(is_safe=False)
def first(value)
▶ def floatformat(text, arg=-1) @register.filter(is_safe=True) Display a float to a specified number of decimal places.
▷ def force_escape(value) @register.filter(is_safe=True) @stringfilter Escape a string's HTML. Return a new string containing the escaped characters (as opposed to "escape", which marks the content for later possible escaping).
@register.filter(is_safe=True)
@stringfilter
def force_escape(value)
▶ def get_digit(value, arg) @register.filter(is_safe=False) Given a whole number, return the requested digit of it, where 1 is the
@register.filter(is_safe=False)
def get_digit(
value,
arg,
)
right-most digit, 2 is the second-right-most digit, etc. Return the
original value for invalid input (if input or argument is not an integer,
or if argument is less than 1). Otherwise, output is always an integer.
▷ def iriencode(value) @register.filter(is_safe=True) @stringfilter Escape an IRI value for use in a URL.
@register.filter(is_safe=True)
@stringfilter
def iriencode(value)
▷ def join(value, arg, autoescape=True) @register.filter(is_safe=True, needs_autoescape=True) Join a list with a string, like Python's ``str.join(list)``.
@register.filter(is_safe=True, needs_autoescape=True)
def join(
value,
arg,
autoescape=True,
)
▷ def json_script(value, element_id) @register.filter(is_safe=True) Output value JSON-encoded, wrapped in a <script type="application/json"> tag.
@register.filter(is_safe=True)
def json_script(
value,
element_id,
)
▷ def last(value) @register.filter(is_safe=True) Return the last item in a list.
@register.filter(is_safe=True)
def last(value)
▷ def length(value) @register.filter(is_safe=False) Return the length of the value - useful for lists.
@register.filter(is_safe=False)
def length(value)
▷ def length_is(value, arg) @register.filter(is_safe=False) Return a boolean of whether the value's length is the argument.
@register.filter(is_safe=False)
def length_is(
value,
arg,
)
▷ def linebreaks_filter(value, autoescape=True) @register.filter("linebreaks", is_safe=True, needs_autoescape=True) @stringfilter Replace 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>``).
@register.filter("linebreaks", is_safe=True, needs_autoescape=True)
@stringfilter
def linebreaks_filter(
value,
autoescape=True,
)
▶ def linebreaksbr(value, autoescape=True) @register.filter(is_safe=True, needs_autoescape=True) @stringfilter Convert all newlines in a piece of plain text to HTML line breaks (``<br>``).
@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def linebreaksbr(
value,
autoescape=True,
)
Reexports
▷ def linenumbers(value, autoescape=True) @register.filter(is_safe=True, needs_autoescape=True) @stringfilter Display text with line numbers.
@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def linenumbers(
value,
autoescape=True,
)
▷ def ljust(value, arg) @register.filter(is_safe=True) @stringfilter Left-align the value in a field of a given width.
@register.filter(is_safe=True)
@stringfilter
def ljust(
value,
arg,
)
▷ def lower(value) @register.filter(is_safe=True) @stringfilter Convert a string into all lowercase.
@register.filter(is_safe=True)
@stringfilter
def lower(value)
▶ def make_list(value) @register.filter(is_safe=False) @stringfilter Return the value turned into a list.
@register.filter(is_safe=False)
@stringfilter
def make_list(value)
For an integer, it's a list of digits.
For a string, it's a list of characters.
▷ def phone2numeric_filter(value) @register.filter("phone2numeric", is_safe=True) Take a phone number and converts it in to its numerical equivalent.
@register.filter("phone2numeric", is_safe=True)
def phone2numeric_filter(value)
▶ def pluralize(value, arg='s') @register.filter(is_safe=False) Return a plural suffix if the value is not 1, '1', or an object of length 1. By default, use 's' as the suffix:
@register.filter(is_safe=False)
def pluralize(
value,
arg='s',
)
* If value is 0, vote{{ value|pluralize }} display "votes".
* If value is 1, vote{{ value|pluralize }} display "vote".
* If value is 2, vote{{ value|pluralize }} display "votes".
If an argument is provided, use that string instead:
* If value is 0, class{{ value|pluralize:"es" }} display "classes".
* If value is 1, class{{ value|pluralize:"es" }} display "class".
* If value is 2, class{{ value|pluralize:"es" }} display "classes".
If the provided argument contains a comma, use the text before the comma
for the singular case and the text after the comma for the plural case:
* If value is 0, cand{{ value|pluralize:"y,ies" }} display "candies".
* If value is 1, cand{{ value|pluralize:"y,ies" }} display "candy".
* If value is 2, cand{{ value|pluralize:"y,ies" }} display "candies".
▶ def pprint(value) @register.filter(is_safe=True) A wrapper around pprint.pprint -- for debugging, really.
@register.filter(is_safe=True)
def pprint(value)
Reexports
▷ def random(value) @register.filter(is_safe=True) Return a random item from the list.
@register.filter(is_safe=True)
def random(value)
▷ def rjust(value, arg) @register.filter(is_safe=True) @stringfilter Right-align the value in a field of a given width.
@register.filter(is_safe=True)
@stringfilter
def rjust(
value,
arg,
)
▷ def safe(value) @register.filter(is_safe=True) @stringfilter Mark the value as a string that should not be auto-escaped.
@register.filter(is_safe=True)
@stringfilter
def safe(value)
▷ def safeseq(value) @register.filter(is_safe=True) A "safe" filter for sequences. Mark each element in the sequence, individually, as safe, after converting them to strings. Return a list with the results.
@register.filter(is_safe=True)
def safeseq(value)
▷ def slice_filter(value, arg) @register.filter("slice", is_safe=True) Return a slice of the list using the same syntax as Python's list slicing.
@register.filter("slice", is_safe=True)
def slice_filter(
value,
arg,
)
▷ def slugify(value) @register.filter(is_safe=True) @stringfilter Convert to ASCII. Convert spaces to hyphens. Remove characters that aren't alphanumerics, underscores, or hyphens. Convert to lowercase. Also strip leading and trailing whitespace.
@register.filter(is_safe=True)
@stringfilter
def slugify(value)
▷ def stringfilter(func) Decorator for filters which should only receive strings. The object passed as the first positional argument will be converted to a string.
▶ def stringformat(value, arg) @register.filter(is_safe=True) Format the variable according to the arg, a string formatting specifier.
▷ def striptags(value) @register.filter(is_safe=True) @stringfilter Strip all [X]HTML tags.
▷ def time(value, arg=None) @register.filter(expects_localtime=True, is_safe=False) Format a time according to the given format.
@register.filter(expects_localtime=True, is_safe=False)
def time(
value,
arg=None,
)
▷ def timesince_filter(value, arg=None) @register.filter("timesince", is_safe=False) Format a date as the time since that date (i.e. "4 days, 6 hours").
@register.filter("timesince", is_safe=False)
def timesince_filter(
value,
arg=None,
)
▷ def timeuntil_filter(value, arg=None) @register.filter("timeuntil", is_safe=False) Format a date as the time until that date (i.e. "4 days, 6 hours").
@register.filter("timeuntil", is_safe=False)
def timeuntil_filter(
value,
arg=None,
)
▷ def title(value) @register.filter(is_safe=True) @stringfilter Convert a string into titlecase.
@register.filter(is_safe=True)
@stringfilter
def title(value)
▷ def truncatechars(value, arg) @register.filter(is_safe=True) @stringfilter Truncate a string after `arg` number of characters.
@register.filter(is_safe=True)
@stringfilter
def truncatechars(
value,
arg,
)
▷ def truncatechars_html(value, arg) @register.filter(is_safe=True) @stringfilter Truncate HTML after `arg` number of chars. Preserve newlines in the HTML.
@register.filter(is_safe=True)
@stringfilter
def truncatechars_html(
value,
arg,
)
▷ def truncatewords(value, arg) @register.filter(is_safe=True) @stringfilter Truncate a string after `arg` number of words. Remove newlines within the string.
@register.filter(is_safe=True)
@stringfilter
def truncatewords(
value,
arg,
)
▷ def truncatewords_html(value, arg) @register.filter(is_safe=True) @stringfilter Truncate HTML after `arg` number of words. Preserve newlines in the HTML.
@register.filter(is_safe=True)
@stringfilter
def truncatewords_html(
value,
arg,
)
▶ def unordered_list(value, autoescape=True) @register.filter(is_safe=True, needs_autoescape=True) Recursively take a self-nested list and return an HTML unordered list -- WITHOUT opening and closing <ul> tags.
@register.filter(is_safe=True, needs_autoescape=True)
def unordered_list(
value,
autoescape=True,
)
Assume the list is in the proper format. For example, if ``var`` contains:
``['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']]``, then
``{{ var|unordered_list }}`` returns::
<li>States
<ul>
<li>Kansas
<ul>
<li>Lawrence</li>
<li>Topeka</li>
</ul>
</li>
<li>Illinois</li>
</ul>
</li>
▷ def upper(value) @register.filter(is_safe=False) @stringfilter Convert a string into all uppercase.
@register.filter(is_safe=False)
@stringfilter
def upper(value)
▶ def urlencode(value, safe=None) @register.filter(is_safe=False) @stringfilter Escape a value for use in a URL.
@register.filter(is_safe=False)
@stringfilter
def urlencode(
value,
safe=None,
)
The ``safe`` parameter determines the characters which should not be
escaped by Python's quote() function. If not provided, use the default safe
characters (but an empty string can be provided when *all* characters
should be escaped).
▷ def urlize(value, autoescape=True) @register.filter(is_safe=True, needs_autoescape=True) @stringfilter Convert URLs in plain text into clickable links.
@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def urlize(
value,
autoescape=True,
)
▶ def urlizetrunc(value, limit, autoescape=True) @register.filter(is_safe=True, needs_autoescape=True) @stringfilter Convert URLs into clickable links, truncating URLs to the given character limit, and adding 'rel=nofollow' attribute to discourage spamming.
@register.filter(is_safe=True, needs_autoescape=True)
@stringfilter
def urlizetrunc(
value,
limit,
autoescape=True,
)
Argument: Length to truncate URLs to.
▷ def wordcount(value) @register.filter(is_safe=False) @stringfilter Return the number of words.
@register.filter(is_safe=False)
@stringfilter
def wordcount(value)
▷ def wordwrap(value, arg) @register.filter(is_safe=True) @stringfilter Wrap words at `arg` line length.
@register.filter(is_safe=True)
@stringfilter
def wordwrap(
value,
arg,
)
▶ def yesno(value, arg=None) @register.filter(is_safe=False) Given a string mapping values for true, false, and (optionally) None, return one of those strings according to the value:
@register.filter(is_safe=False)
def yesno(
value,
arg=None,
)
========== ====================== ==================================
Value Argument Outputs
========== ====================== ==================================
``True`` ``"yeah,no,maybe"`` ``yeah``
``False`` ``"yeah,no,maybe"`` ``no``
``None`` ``"yeah,no,maybe"`` ``maybe``
``None`` ``"yeah,no"`` ``"no"`` (converts None to False
if no mapping for None is given.
========== ====================== ==================================