๐Ÿ“ Django

Django Template Filters

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
278
๐ŸŒฟ
Level
Medium

Filters are applied to template variables using the | pipe character.

Strings

{{ name|lower }}              <!-- dmitrii -->
{{ name|upper }}              <!-- DMITRII -->
{{ name|title }}              <!-- Dmitrii Masliaev -->
{{ name|capfirst }}           <!-- Dmitrii -->
{{ text|truncatewords:10 }}   <!-- first 10 words... -->
{{ text|truncatechars:50 }}   <!-- first 50 characters... -->
{{ text|wordcount }}          <!-- word count -->
{{ html|striptags }}          <!-- strip HTML tags -->
{{ text|linebreaks }}         <!-- \n โ†’ <p> -->
{{ text|linebreaksbr }}       <!-- \n โ†’ <br> -->

Numbers

{{ price|floatformat:2 }}     <!-- 1234.56 -->
{{ count|pluralize }}         <!-- "s" or "" -->
{{ count|pluralize:"y,ies" }}    <!-- category/categories -->
{{ number|filesizeformat }}   <!-- 1.2 MB -->

Dates

{{ date|date:"d.m.Y" }}       <!-- 15.01.2024 -->
{{ date|date:"d M Y" }}       <!-- 15 Jan 2024 -->
{{ date|time:"H:i" }}         <!-- 10:30 -->
{{ date|timesince }}          <!-- 3 days ago -->
{{ date|timeuntil }}          <!-- in 2 days -->

Lists and Dictionaries

{{ items|length }}            <!-- number of items -->
{{ items|first }}             <!-- first item -->
{{ items|last }}              <!-- last item -->
{{ items|join:", " }}         <!-- comma-separated -->
{{ items|slice:":3" }}        <!-- first 3 items -->
{{ items|dictsort:"name" }}   <!-- sort by field -->

Logic and Display

{{ value|default:"not specified" }}    <!-- if False/None/empty -->
{{ value|default_if_none:"N/A" }}      <!-- only if None -->
{{ value|yesno:"yes,no,unknown" }}     <!-- True/False/None -->

Security

{{ html|safe }}               <!-- do not escape HTML -->
{{ text|escape }}             <!-- escape (default behaviour) -->
{{ text|urlize }}             <!-- convert URLs to links -->

Custom Filter

# templatetags/my_filters.py
from django import template
register = template.Library()

@register.filter
def priority_label(value):
    labels = {1: 'Low', 2: 'Medium', 3: 'High'}
    return labels.get(value, value)
{% load my_filters %}
{{ task.priority|priority_label }}

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 279
๐Ÿ“

Swagger Documentation with drf-spectacular

drf-spectacular generates an OpenAPI 3.0 schema and Swagger UI for DRF.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 561
๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum Markup for Backend Developers Open course curriculum