๐ Introduction
Django provides template filters to modify data before displaying it.
๐ Filters are used with | operator.
๐ฏ Program Statement
๐ Demonstrate template filters such as upper, lower, length, and date.
๐ง What are Template Filters?
๐ Template filters:
- Transform data
- Format output
- Improve display
โ๏ธ Step 1: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
from datetime import datetime
def filter_demo(request):
name = "Kavita Srivastava"
course = "Django Programming"
subjects = ["Python", "Java", "AI"]
today = datetime.now()
return render(request, 'filters.html', {
'name': name,
'course': course,
'subjects': subjects,
'today': today
})
โ๏ธ Step 2: URL Mapping
๐ File: urls.py
๐น Path:
myproject/myproject/urls.py
๐น Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('filters/', views.filter_demo),
]
โ๏ธ Step 3: Create Template
๐ File: filters.html
๐น Path:
myproject/templates/filters.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Template Filters</title>
</head>
<body>
<h1>Template Filters Demo</h1>
<!-- Upper Filter -->
<p>Uppercase: {{ name|upper }}</p>
<!-- Lower Filter -->
<p>Lowercase: {{ course|lower }}</p>
<!-- Length Filter -->
<p>Number of Subjects: {{ subjects|length }}</p>
<!-- Date Filter -->
<p>Today: {{ today|date:"d-m-Y" }}</p>
</body>
</html>
โ๏ธ Step 4: Run Server
python manage.py runserver
๐ Step 5: Output
๐ http://127.0.0.1:8000/filters/
โ Output:
Uppercase: KAVITA SRIVASTAVA
Lowercase: django programming
Number of Subjects: 3
Today: 09-04-2026
๐ง How It Works
- View sends data
- Template applies filters
- Data is modified before display
๐ฅ Important Filters Explained
โ 1. Upper
{{ name|upper }}
๐ Converts text to uppercase
โ 2. Lower
{{ course|lower }}
๐ Converts text to lowercase
โ 3. Length
{{ subjects|length }}
๐ Returns number of items
โ 4. Date
{{ today|date:"d-m-Y" }}
๐ Formats date
๐ง General Syntax
{{ variable|filter }}
โ ๏ธ Common Errors
โ Missing quotes in date
โ Wrong:
{{ today|date:d-m-Y }}
โ Correct:
{{ today|date:"d-m-Y" }}
โ Applying filter on wrong type
๐ Example:
{{ subjects|upper }} โ
โ Variable not passed
๐ Ensure:
'today': today
๐งช Practice Questions
- Use
titlefilter - Display first item of list
- Format date as
Month Day, Year - Chain filters (advanced)
๐ค Viva Questions & Answers
1. What are template filters?
Template filters are used to modify or format data before displaying it in templates.
2. What is the syntax of filter?
{{ variable|filter }} is used to apply filter.
3. What does upper filter do?
It converts text into uppercase.
4. What does lower filter do?
It converts text into lowercase.
5. What does length filter do?
It returns number of elements in list or characters in string.
6. What does date filter do?
It formats date into a specified format.
7. Can we chain filters?
Yes, multiple filters can be applied together.
8. What happens if filter is invalid?
Django may ignore or throw error depending on case.
9. Can filters be applied on all data types?
No, filters work based on data type compatibility.
10. Why are filters important?
They improve presentation and formatting of data.
๐ Next Post: Use {% url %} and {% static %} Tags
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
