Template Filters Example
A snippet showing how to use built-in Django template filters.
# models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=150)
published_at = models.DateTimeField()
def __str__(self):
return self.title
# templates/post_item.html
<h2>{{ post.title|upper }}</h2>
<p>Published: {{ post.published_at|date:"M d, Y" }}</p>
<p>Title length: {{ post.title|length }}</p>
Explanation:
-
Filters transform values inline, e.g.,
|lower,|upper,|date,|length. -
Chain filters:
{{ name|default:'Anon'|upper }}. - Prefer formatting in templates; keep business logic in Python.
- Category Templates
- Total Views 1126
- Last Modified 22 January, 2026
- Tags #templates #filters #built-in #format
Previous snippet
Include Other Templates
Next snippet