Skip to content

Instantly share code, notes, and snippets.

@josylad
Last active November 29, 2023 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josylad/3ed636e20453519992a768bd79a0115c to your computer and use it in GitHub Desktop.
Save josylad/3ed636e20453519992a768bd79a0115c to your computer and use it in GitHub Desktop.
How to use Django Template Filters

Join Filter

<!-- the value -->
[‘python’, ‘is’, ‘fun'] | join:" - "
<!-- the output -->
"python - is - fun"
<!--  the value -->
[‘Monday’, ‘Tuesday’, ‘Wednesday'] | join:" // "
<!-- the output -->
"Monday // Tuesday // Wednesday"

Date Filter

<!-- the value -->
2023-01-12T10:30:00.000123 | date:"SHORT_DATE_FORMAT”
<!-- the output -->
‘01/12/2023’
<!-- the value -->
2022-12-12T10:30:00.000123 | date
<!-- the output -->
‘Dec. 12, 2022’
<!--  the value -->
2023-01-12T10:30:00.000123 | date:"D d M Y"
<!--  the output -->
Sun 12 Jan 2023’

Default Filter

<!-- the value -->
“ “ | default: "an empty string"
<!-- the output -->
‘an empty string’
<!-- the value -->
“ “ | default: "This is a default value"
<!--  the output if the value is an empty string -->
‘This is a default value’

Add Filter

<!-- the value -->
20 | add:"5"
<!-- the output -->
25
<!--  the value  -->
[1, 2, 3] | add:”[4, 5, 6]”
<!--  the output -->
[1, 2, 3, 4, 5, 6]

Capfirst Filter

<!-- the value -->
‘christmas’ | capfirst
<!-- the output -->
‘Christmas’
<!--  the value -->
‘django’ | capfirst
<!--  the output -->
‘Django’

Cut Filter

<!-- the value -->
“Python is Fun” | cut:" " 
<!-- the output -->
‘PythonisFun’
<!--  the value  -->
“January - February - March” | cut:"-" 
<!-- the output  -->
‘January February March’

Dictsort Filter

[
    {'name': ‘Josh’, 'age': 19},
    {'name': ‘Dave’, 'age': 22},
    {'name': 'joe', 'age': 31},
]

then the output would be:

[
{'name': ‘Dave’, 'age': 22},
{'name': 'joe', 'age': 31},
{'name': ‘Josh’, 'age': 19},

]
<!--  the value  -->
[
   {'name': 'Abhi', 'age': 29},
   {'name': 'Sonia', 'age': 25},
   {'name': 'Rahul', 'age': 36},
]
<!--  the output  -->
[
   {'name': 'Abhi', 'age': 29},
   {'name': 'Rahul', 'age': 36},
   {'name': 'Sonia', 'age': 25},
]

Escape Filter

<!-- the value -->
<p>This <em>should be</em> fun!</p> | escape
<!-- the output -->
<p>This <em>should be</em> fun!</p>

First Filter

<!-- the value -->
 [‘Apple’, ‘Mango’, ‘Orange’] | first
<!-- the output -->
‘Apple’
<!--  the value -->
 [‘China’, ‘India’, ‘Thailand’] | first
<!--  the output -->
‘China’

Last Filter

<!-- the value -->
 [‘Apple’, ‘Mango’, ‘Orange’] | last
<!-- the output -->
‘Orange’
<!--  the value -->
 [‘China’, ‘India’, ‘Thailand’] | last
<!--  the output -->
‘Thailand’

Length Filter

<!-- the value -->
 [‘Banana’, ‘Mango’, ‘Orange’] | length
<!-- the output -->
3
<!--  the value -->
 “Welcome” | length
<!--  the output -->
7

linenumbers filter

cat
dog
horse

The output will be:

1. cat
2. dog
3. horse

If the value is:

Arnold
Brandy
Caleb
Dexter

The output will be:

1. Arnold
2. Brandy
3. Caleb
4. Dexter

Lower Filter

<!-- the value -->
‘I Am Master Yoda’ | lower
<!-- the output -->
 ‘i am master yoda ‘
<!--  the value -->
‘Humpty Dumpty sat on the Wall’ | lower
<!--  the output -->
 ‘humpty dumpty sat on the wall‘

Upper Filter

<!-- the value -->
‘Happy coding!’ | upper
<!-- the output -->
‘HAPPY CODING!’
<!--  the value -->
‘Nasa’ | upper
<!--  the output -->
‘NASA’

Title Filter

<!-- the value -->
‘I am a legend’ | title
<!-- the output -->
‘I Am A Legend’
<!--  the value -->
‘Django rest framework’ | title
<!--  the output -->
‘Django Rest Framework’

Random Filter

<!-- the value -->
[‘Banana’, ‘Mango’, ‘Orange’] | random
<!-- the output -->
‘Banana or Mango or Orange’
<!--  the value -->
[‘Father’, ‘Mother’, ‘Child’] | random
<!--  the output -->
‘Father or Mother or Child’

Slice Filter

<!-- the value -->
[‘Banana’, ‘Mango’, ‘Orange’] | slice:":2"
<!-- the output -->
[‘Banana’, ‘Mango’]
<!--  the value -->
[‘Father’, ‘Mother’, ‘Child’] | slice:":2"
<!--  the output -->
[‘Father’, ‘Mother’] 

Time Filter

<!-- the value -->
10:30:00.000123+02:00 | time:"H:i"
<!-- the output -->
10:30
<!--  the value -->
12:30:00.000123+02:00 | time:"H\h i\m"
<!-- the output -->
“12h 30m”

Timesince Filter

<!-- the value -->
2022-01-02T10:30:00.000123 | timesince
<!-- the output at 2022-01-02T12:30:00.000123 -->
‘2 hours’
<!--  the value -->
2023-01-02T9:30:00.000123 | timesince
<!--  the output at 2023-01-02T12:30:00.000123 -->
‘3 hours’

Truncatechars Filter

<!-- the value -->
‘Happy coding’ | truncatechars:6
<!-- the output -->
‘Happy…’
<!--  the value -->
‘Cloud computing’ | truncatechars:5
<!--  the output  -->
‘Cloud…’

Other Template Filters

Wordcount Filter

<!-- the value -->
‘Happy coding’ | wordcount
<!-- the output -->
2
<!-- the value  -->
‘You can do a lot of modifications with template filters’ | wordcount
<!-- the output -->
10

Truncatewords Filter

<!-- the value -->
‘Better days ahead!’ | truncatewords:2
<!-- the output -->
‘Better days’
<!--  the value -->
“You can build web apps with python and Django” | truncatewords:7
<!--  the output -->
“You can build web apps with python”

Striptags Filter

<!-- the value -->
<b>I</b> <button>love</button> <span>dogs</span> | striptags
<!-- the output -->
‘I love dogs’
<!--  the value -->
<b>This</b> is <em>a</em> <button>Button</button> | striptags
<!--  the output -->
‘This is a Button’

Pluralize Filter

You have {{ num_messages }} message {{ num_messages | pluralize }}.
You have {{ num_tomato }} tomato {{ num_tomato | pluralize:"es" }}.
You have {{ num_cherries }} cherr {{ num_cherries | pluralize:"y,ies" }}.
<!--  the value -->
You have {{ num_messages }} message {{ num_messages | pluralize }}.
<!--  the output if the value of num_messages is 4 -->
“You have 4 messages”

Make_list Filter

<!-- the value -->
‘Earthly’ | make_list
<!-- the output -->
[‘E’, ‘a’, ‘r’, ‘t’, ‘h’, ‘l’, ‘y’]
<!--  the value -->
‘ABCDEF’ | make_list
<!--  the output -->
[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’]
{% extends ‘base.html' %}
{% load titlecolor_filter %}
<div class="col-lg-7">
<div class="container">
<!-- the actual blog post: title/author/date/content -->
<div style="color: {{article.title | title_color}};"> <h1> {{article.title}} </h1> </div>
<hr>
<p> <i class="fa fa-calendar"></i> Published on {{article.date_posted|date}}</p>
<hr>
<p>{{article.content}}</p>
</div>
</div>
from django import template
register = template.Library()
@register.filter()
def title_color(value):
if value:
return "#FF0000"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment