Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 4 of 10 · Django Templates
Interview question

What are Django template filters and how do you create a custom filter? Django template filters क्या हैं और custom filter कैसे बनाएं?

Answer

Filters transform a variable's displayed value using the pipe (|) syntax. Django ships with many built-in filters, and custom ones can be registered for app-specific formatting needs.

{{ book.title|upper }}                    <!-- UPPERCASE -->
{{ book.title|lower }}                    <!-- lowercase -->
{{ book.title|truncatewords:5 }}          <!-- first 5 words -->
{{ book.price|floatformat:2 }}            <!-- 25.99 -->
{{ book.description|default:'No description' }}  <!-- fallback if empty/falsy -->
{{ book.description|striptags }}          <!-- removes HTML tags -->
{{ books|length }}                        <!-- count of items -->
{{ book.published_date|date:'F j, Y' }}   <!-- January 1, 2024 -->

# Chaining filters - applied left to right
{{ book.title|lower|truncatewords:3 }}

# CUSTOM FILTER - defined in a templatetags module
# myapp/templatetags/__init__.py  (empty file)
# myapp/templatetags/custom_filters.py
from django import template

register = template.Library()

@register.filter(name='currency')
def currency(value):
    return f'${value:,.2f}'

@register.filter
def discount(value, percent):  # filters can take ONE argument
    return value * (1 - percent / 100)

# Using the custom filter in a template
{% load custom_filters %}  <!-- must load the module first -->

<p>{{ book.price|currency }}</p>          <!-- $25.99 -->
<p>{{ book.price|discount:10|currency }}</p>  <!-- 10% off, then formatted -->

# is_safe and needs_autoescape - important flags for filters that
# return HTML, to control Django's automatic escaping behavior
@register.filter(is_safe=True)
def bold_title(value):
    from django.utils.safestring import mark_safe
    return mark_safe(f'<strong>{value}</strong>')

Filters pipe (|) syntax से variable के displayed value को transform करते हैं। Django में कई built-in filters हैं, custom filters app-specific formatting के लिए register हो सकते हैं।

{{ book.title|upper }}
{{ book.title|lower }}
{{ book.title|truncatewords:5 }}
{{ book.price|floatformat:2 }}
{{ book.description|default:'No description' }}
{{ book.description|striptags }}
{{ books|length }}
{{ book.published_date|date:'F j, Y' }}

{{ book.title|lower|truncatewords:3 }}

# CUSTOM FILTER
# myapp/templatetags/custom_filters.py
from django import template

register = template.Library()

@register.filter(name='currency')
def currency(value):
    return f'${value:,.2f}'

@register.filter
def discount(value, percent):
    return value * (1 - percent / 100)

# Template में custom filter use करना
{% load custom_filters %}

<p>{{ book.price|currency }}</p>
<p>{{ book.price|discount:10|currency }}</p>

@register.filter(is_safe=True)
def bold_title(value):
    from django.utils.safestring import mark_safe
    return mark_safe(f'<strong>{value}</strong>')

Was this answer clear?