Use built-in Django filters on Django templates


You want to format Django template variables so they're displayed in a different manner than how they're passed by Django view methods or url option parameters. Django offers filters to format dates, numbers, strings, special characters, among other things.


Use one of Django's many built-in filters. Django offers built-in filters for several purposes that are available by default on all Django templates.

How it works

Django filters are designed to format individual template variables, unlike Django tags which take effect on entire template sections. The syntax to apply Django filters to template variables is a 'pipe' represented by the vertical bar character |. The syntax to apply a Django filter is the following {{variable|filter}}. In addition, it's also possible to apply multiple filters to the same variable (e.g.{{variable|filter|filter}}).

I'll classify each Django built-in tag into functional sections so it's easier to identify them. The functional classes I'll use are: Dates, Strings, Numbers, Lists, Dictionaries, Spacing and special characters, Development and Testing & Urls.

Note Apply Django filters to entire sections with the {% filter %} tag

Because Django filters operate on a granular level, it can be time consuming to apply filters to multiple variables. You can use the {% filter %} tag to apply filters to multiple variables that span a template section. See the previous recipe Use built-in Django tags on Django templates - {% filter %} tag.


Note date filter also accepts predefined date variables

The date filter also accepts predefined date variables. For example, {{variable|date:"DATE_FORMAT"}}, {{variable|date:"DATETIME_FORMAT"}}, {{variable|date:"SHORT_DATE_FORMAT" %} or {{variable|date:"SHORT_DATETIME_FORMAT"}}.

The date variables in themselves are also composed of date strings. For example DATE_FORMAT default's to "N j, Y" (e.g. Jan 1, 2015), DATETIME_FORMAT defaults to "N j, Y, P" (e.g. Jan 1, 2015, 12 a.m.), SHORT_DATE_FORMAT defaults to "m/d/Y" (e.g. 01/01/2015) and SHORT_DATETIME_FORMAT defaults to "m/d/Y P" (e.g. 01/01/2015 12 a.m.). Each date variable can be overridden with different date strings in a project's file.

Table 1 - Django date filter characters
Standardized charactersDescription
cOutputs ISO 8601 format (e.g. 2015-01-02T10:30:00.000123+02:00 or 2015-01-02T10:30:00.000123 if the datetime has no timezone [i.e.naive datetime])
rOutputs RFC 2822 formatted date (e.g.'Thu, 21 Dec 2000 16:01:07 +0200')
UOutputs seconds since Unix epoch date--January 1 1970 00:00:00 UTC
Hour characters 
aOutputs 'a.m.' or 'p.m.'
AOutputs 'AM' or 'PM'
fOutputs time, 12-hour hours and minutes, with minutes left off if they're zero (e.g.'1', '1:30')
gOutputs hour, 12-hour format without leading zeros (e.g.'1' to '12')
GOutputs hour, 24-hour format without leading zeros (e.g.'0' to '23')
hOutputs hour, 12-hour format (e.g.'01' to '12')
HOutputs hour, 24-hour format (e.g.'00' to '23')
iOutputs minutes (e.g.'00' to '59')
POutputs time, 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off if they're zero and the special-case strings 'midnight' and 'noon' if appropriate (e.g.'1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.')
sOutputs seconds, 2 digits with leading zeros (e.g.'00' to '59')
uOutputs microseconds (e.g.000000 to 999999)
Timezone characters 
eOutputs timezone name. Could be in any format, or might return an empty string, depending on the datetime (e.g. '', 'GMT', '-500', 'US/Eastern')
OOutputs difference to Greenwich time in hours (e.g.'+0200')
TOutputs system time zone (e.g.'EST', 'MDT')
ZOutputs time zone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive (e.g.-43200 to 43200)
Day and week characters 
DOutputs day of the week, textual, 3 letters (e.g.'Thu','Fri')
lOutputs day of the week, textual, long (e.g.'Thursday','Friday')
SOutputs English ordinal suffix for day of the month, 2 characters (e.g.'st', 'nd', 'rd' or 'th')
wOutputs day of the week, digits without leading zeros (e.g.'0' for Sunday to '6' for Saturday)
zOutputs day of the year (e.g.0 to 365)
WOutputs the week number of the year, with weeks starting on Monday based on ISO-8601 (e.g.1, 53)
oOutputs week-numbering year, corresponding to the ISO-8601 week number (W)(e.g.'1999')
Month characters 
bOutputs textual month, 3 letters, lowercase (e.g.'jan','feb')
dOutputs day of the month, 2 digits with leading zeros (e.g.'01' to '31')
jOutputs day of the month without leading zeros (e.g. '1' to '31')
EOutputs month, locale specific alternative representation usually used for long date representation (e.g. 'listopada' for Polish locale, as opposed to 'Listopad')
FOutputs month, textual, long (e.g. 'January','February')
mOutputs month, 2 digits with leading zeros (e.g.'01' to '12')
MOutputs month, textual, 3 letters (e.g.'Jan','Feb')
nOutputs month without leading zeros (e.g.'1' to '12')
NOutputs month abbreviation in Associated Press style (e.g.'Jan', 'Feb', 'March', 'May')
tOutputs number of days in the given month (e.g. 28 to 31)
Year characters 
LOutputs boolean for whether it's a leap year (e.g.True or False)
yOutputs year, 2 digits (e.g.'99')
YOutputs year, 4 digits (e.g.'1999')

Strings, lists and numbers


Table 2 - Django floatformat filter


Listing 1 - Django linenumbers filter

# Variable definition 

# Template definition with linenumbers filter

# Output

Lists and dictionaries

Listing 2 - Django unordered_list filter

# Variable definition 
["Stores",["San Diego",["Downtown","Uptown","Midtown"]]]

# Template definition with linenumbers filter

# Output
       <li>San Diego

* Note the first level does not include opening or closing <ul> tags

Spacing and special characters

Note Use the {% autoescape %} tag for multiple variables or autoescape in OPTIONS to disable autoescaping globally

If you use the escape filter multiple times, it's easier to create a template section wrapped with the {% autoescape %} tag to achieve the same result. See the previous recipe Use built-in Django tags on Django templates - {% autoescape %} tag.

If you want to disable autoescaping globally (i.e. on all templates), it's easier to disable it at the project level using the autoescape field in the OPTIONS variable in the TEMPLATES configuration, inside a project's file. See the previous recipe Customize Django template configuration - autoescape OPTIONS.

Listing 3 - Django wordwrap filter

# Variable definition 
Coffeehouse started as a small store

# Template definition with wordwrap filter for every 12 characters

# Output
started as a
small store

Development and testing