Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
Django Template Filters
Next article icon

Django Template Filters

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Django Template Engine provides filters which are used to transform the values of variables and tag arguments. We have already discussed major Django Template Tags. Tags can't modify value of a variable whereas filters can be used for incrementing value of a variable or modifying it to one's own need.

Syntax

{{ variable_name | filter_name }}

Filters can be “chained.” The output of one filter is applied to the next. {{ text|escape|linebreaks }} is a common idiom for escaping text contents, then converting line breaks to <p> tags.

Example

{{ value | length }}

If value is ['a', 'b', 'c', 'd'], the output will be 4.

Major Template Filters in Django

This article revolves around various Django Template Filters one can use during a project. Filters transform the values of variables and tag arguments. Let's check some major filters.

addaddslashescapfirst
centercutdate
defaultdictsortdivisibleby
escapefilesizeformatfirst
joinlastlength
linenumberslowermake_list
randomsliceslugify
timetimesincetitle
unordered_listupperwordcount

add

  1. It is used to add an argument to the value. Example
{{ value | add:"2" }}
  1. If value is 4, then the output will be 6. This filter is used to increment a variable in django Templates.

addslashes

  1. It is used to add slashes before quotes. Useful for escaping strings in CSV. Example
{{ value | addslashes }}
  1. If value is "I'm Jai", the output will be "I\'m Jai".

capfirst

  1. It is used to capitalize the first character of the value. If the first character is not a letter, this filter has no effect. Example
{{ value | capfirst }}
  1. If value is "jai", the output will be "Jai".

center

  1. It is used to center the value in a field of a given width. Example
"{{ value | center:"15" }}"
  1. If value is "Jai", the output will be " Jai ".

cut

  1. It is used to remove all values of arg from the given string. Example
{{ value | cut:" " }}
  1. If value is "String with spaces", the output will be "Stringwithspaces".

date

  1. It is used to format a date according to the given format. Example
{{ value | date:"D d M Y" }}
  1. If value is a datetime object (e.g., the result of datetime.datetime.now()), the output will be the string 'Thu 06 Feb 2020'. For More information and patterns visit here.

default

  1. It is used to give a default value to a variable. If variable evaluates to False, it will use the default argument given else the variable value itself. Example
{{ value | default:"nothing" }}
  1. If value is "" (the empty string), the output will be nothing.

dictsort

  1. It takes a list of dictionaries and returns that list sorted by the key given in the argument. Example
{{ value | dictsort:"name" }}
  1. If value is:
[     {'name': 'zed', 'age': 19},     {'name': 'amy', 'age': 22},     {'name': 'joe', 'age': 31}, ]
  1. then the output would be:
[     {'name': 'amy', 'age': 22},     {'name': 'joe', 'age': 31},     {'name': 'zed', 'age': 19}, ] 

divisibleby

  1. It returns True if the value is divisible by the argument. Example
{{ value | divisibleby:"3" }}
  1. If value is 21, the output would be True.

escape

  1. It is used to escape a string’s HTML. Specifically, it makes these replacements: 
html
< is converted to < > is converted to > ' (single quote) is converted to &#x27; " (double quote) is converted to " & is converted to & 
  1. Example
{{ title | escape }}

filesizeformat

  1. It is used to format the value like a ‘human-readable’ file size (i.e. '13 KB', '4.1 MB', '102 bytes', etc.). Example
{{ value | filesizeformat }}
  1. If value is 123456789, the output would be 117.7 MB.

first

  1. It is used to return the first item in a list. Example
{{ value | first }}
  1. If value is the list ['a', 'b', 'c'], the output will be 'a'.

join

  1. It is used to join a list with a string, like Python’s str.join(list) Example
{{ value | join:" // " }}
  1. If value is the list ['a', 'b', 'c'], the output will be the string "a // b // c".

last

  1. It is used to return the last item in a list. Example
{{ value | last }}
  1. If value is the list ['a', 'b', 'c', 'd'], the output will be the string "d".

length

  1. It is used to return the length of the value. This works for both strings and lists. Example
{{ value | length }}
  1. If value is ['a', 'b', 'c', 'd'] or "abcd", the output will be 4.

linenumbers

  1. It is used to display text with line numbers. Example
{{ value | linenumbers }}
  1. If value is:
one two three
  1. the output will be:
1. one 2. two 3. three

lower

  1. It is used to converts a string into all lowercase. Example
{{ value | lower }}}
  1. If value is My Name is Jai, the output will be my name is jai.

make_list

  1. It is used to return the value turned into a list. For a string, it’s a list of characters. For an integer, the argument is cast to a string before creating a list. Example
{{ value | make_list }}
  1. If value is the string "Naveen", the output would be the list ['N', 'a', 'v', 'e', 'e', 'n']. If value is 123, the output will be the list ['1', '2', '3'].

random

  1. It is used to return a random item from the given list. Example
{{ value | random }}
  1. If value is the list ['a', 'b', 'c', 'd'], the output could be "b".

slice

  1. It is used to return a slice of the list. Example
{{ some_list | slice:":2" }}
  1. If some_list is ['a', 'b', 'c'], the output will be ['a', 'b'].

slugify

  1. It is used to convert to ASCII. It converts spaces to hyphens and removes characters that aren’t alphanumerics, underscores, or hyphens. Converts to lowercase. Also strips leading and trailing whitespace. Example
{{ value | slugify }}
  1. If value is "Jai is a slug", the output will be "jai-is-a-slug".

time

  1. It is used to format a time according to the given format. Example
{{ value | time:"H:i" }}
  1. If value is equivalent to datetime.datetime.now(), the output will be the string "01:23".

timesince

  1. It is used to format a date as the time since that date (e.g., “4 days, 6 hours”). Example
{{ blog_date | timesince:comment_date }}
  1. if blog_date is a date instance representing midnight on 1 June 2006, and comment_date is a date instance for 08:00 on 1 June 2006, then the following would return “8 hours”

title

  1. It is used to convert a string into titlecase by making words start with an uppercase character and the remaining characters lowercase. This filter makes no effort to keep “trivial words” in lowercase. Example
{{ value | title }}
  1. If value is "my FIRST post", the output will be "My First Post".

unordered_list

  1. It is used to recursively take a self-nested list and returns an HTML unordered list – WITHOUT opening and closing <ul> tags. Example
{{ var | unordered_list }}
  1. if var contains ['States', ['Kansas', ['Lawrence', 'Topeka'], 'Illinois']], then {{ var|unordered_list }} would return: 
html
<li>States <ul>         <li>Kansas         <ul>                 <li>Lawrence</li>                 <li>Topeka</li>         </ul>         </li>         <li>Illinois</li> </ul> </li>          </li> 

upper

  1. It is used to convert a string into all uppercase. Example
{{ value | upper }}
  1. If value is "Jai is a slug", the output will be "JAI IS A SLUG".

wordcount

  1. It is used to return the number of words. Example
{{ value | wordcount }}
  1. If value is "jai is a slug", the output will be 4.

Next Article
Django Template Filters

N

NaveenArora
Improve
Article Tags :
  • Python
  • Python Django
  • Django-templates
Practice Tags :
  • python

Similar Reads

    Django Templates
    Templates are the third and most important part of Django's MVT Structure. A Django template is basically an HTML file that can also include CSS and JavaScript. The Django framework uses these templates to dynamically generate web pages that users interact with. Since Django primarily handles the ba
    7 min read
    variables - Django Templates
    Prerequisite- Django TemplatesIn Django, templates are used to dynamically generate HTML content by combining static HTML with dynamic data from views. One of the simplest and most useful features of Django templates is the use of variables. Variables allow you to display data passed from a view ins
    2 min read
    Django Template Tags
    Prerequisite: What are Django Templates?Django provides a powerful templating engine that allows us to add logic directly into our templates using template tags. These tags enable everything from control structures (like if and for loops), to inserting dynamic content, to template inheritance. Templ
    4 min read
    extends - Django Template Tags
    Django’s {% extends %} tag allows you to reuse a base template across multiple pages, so you don’t have to repeat the same HTML code in every template. This makes your templates cleaner, easier to maintain, and helps keep a consistent layout throughout your site.Syntax: {% extends 'base_template.htm
    2 min read
    if - Django Template Tags
    The {% if %} tag in Django templates allows us to control what content is displayed based on certain conditions. We can use it to show or hide parts of a page depending on whether a condition is met.Syntax of the {% if %} Tag{% if variable %}// statements{% else %}// statements{% endif %}condition:
    3 min read
    for loop - Django Template Tags
    Django templates allow you to render dynamic data by embedding Python-like logic into HTML. The for loop is one of the most commonly used template tags, enabling you to iterate over lists or other iterable objects. It helps you display repeated content (like lists, tables, etc.) in your templates wi
    3 min read
    comment - Django template tags
    A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to the template but also provide som
    2 min read
    include - Django Template Tags
    A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
    2 min read
    url - Django Template Tag
    A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
    3 min read
    cycle - Django Template Tags
    A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences