python / beginner
Snippet
Creating a Custom Template Filter
Custom filters allow you to transform data directly within Django templates. You register them using a Library instance.
snippet.py
1
2
3
4
5
6
7
from django import templateregister = template.Library()@register.filter(name='shout')def shout(value):return value.upper() + "!!!"
django
Breakdown
1
register = template.Library()
Creates an instance used to register your custom tags and filters.
2
@register.filter(name='shout')
A decorator that registers the following function as a filter named 'shout'.