Django

Demonstrate Template Filters

๐Ÿ“Œ 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

  1. View sends data
  2. Template applies filters
  3. 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

  1. Use title filter
  2. Display first item of list
  3. Format date as Month Day, Year
  4. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *