Django template filters are functions that transform values during template rendering. Pytigon extends the standard Django filter set with its own collection, available globally in all templates.
Pytigon registers template filters through Django's standard mechanism: create a templatetags/ directory in your application with filter modules. Pytigon auto-discovers these during startup.
All Django built-in filters are available in Pytigon templates:
{{ value|default:"nothing" }}
{{ name|lower }}
{{ list|join:", " }}
{{ text|truncatechars:100 }}
{{ date|date:"Y-m-d" }}
{{ count|pluralize }}
Pytigon adds several custom filters for common application patterns. These are defined in the pytigon_lib and pytigon packages and are available globally.
{{ "Hello"|translate }} — Translate using Pytigon's I18N system
{{ "Hello"|translate_to:"pl" }} — Translate to specific language
{{ field_name|field_title }} — Get the verbose name/title of a model field
{{ model_instance|model_name }} — Get the model class name
{{ queryset|to_json }} — Serialize a QuerySet to JSON
{{ value|smart_float }} — Format float with locale-aware separators
{{ value|isinstance:"str" }} — Check Python type (returns bool)
{{ value|getattr:"field_name" }} — Access attribute by string name
{{ dict|get_item:"key" }} — Access dict item by key
Add custom filters to any application's templatetags/ directory:
# [app]/templatetags/my_filters.py
from django import template
register = template.Library()
@register.filter(name='add_prefix')
def add_prefix(value, prefix):
"""Add a prefix to a string."""
return f"{prefix}{value}"
@register.filter(name='status_badge')
def status_badge(value):
"""Convert a status code to a Bootstrap badge HTML."""
badges = {
'active': '<span class="badge bg-success">Active</span>',
'pending': '<span class="badge bg-warning">Pending</span>',
'inactive': '<span class="badge bg-secondary">Inactive</span>',
}
return badges.get(value, value)
Usage in templates:
{{ record.status|status_badge }}
{{ record.code|add_prefix:"ITEM-" }}
Filters are auto-discovered. No configuration needed — just add the file to the right directory.