How to Create your own Template Filter in Django

by Shahid Ayoub

Django’s template language comes with a wide variety of built-in tags and filters designed to address the presentation logic needs of your application. Nevertheless, you may find yourself needing functionality that is not covered by the core set of template primitives. You can extend the template engine by defining custom tags and filters using Python, and then make them available to your templates using the {% load tag_name %} tag.

We come across various situations where we are required to display our template content in a particular fashion or we have to do some calculations on variables available to us when our custom template filters come to our rescue. Django filters are simple Python functions that accept a value, an optional argument, and return a value to be displayed on the page. Creating your own template filters is easy and allows you to do some pretty creative formatting in templates.

The Django app in which you want to use template filters should contain a templatetags directory, at the same level as models.py, views.py, etc. If this doesn’t already exist, create it – don’t forget the __init__.py file to ensure directory is treated as a Python package.

The layout should look like :

site_app/
__init__.py
models.py
templatetags/
__init__.py
filter_strings.py
Views.py

Here site_app is django app and filter_strings.py contains the custom tags/filters.

Now let us take an example of varchar variable to be displayed on the template. But we are required to remove some special characters in the string after 10 characters. We will use custom filter in the template. We start by adding the {% load filter_strings %} tag, which will load the filters specified in the filter_strings.py file. You can also see the filter applied to the string variable:

 {% load app_filters %}
Detail: {{ detail|filter_strings:10 }}

In order to be a valid tag library, the module must contain a module-level variable named register that is a template, library instance, in which all the tags and filters are registered. The custom filter(filter_strings) will look like:

from django import template
register = template.Library()

@register.filter
def filter_strings(value, arg):
    full_string = value
    allowed_length = arg
    processed_string_1 = full_string[: allowed_length]
    processed_string_2 = full_string[ allowed_length:].replace('-',' ')
    processed_string_2 = processed_string_2[ allowed_length:].replace('%’,' ')
    processed_string = processed_string_1 + processed_string_2
    return processed_string

When we render the template now, we can see the special character (- and %) is not present after the allowed length of 10 characters.

Note: This blog is intended for developers working on Django framework specifically.

For any clarification or feedback leave your comment below or email me at ayoub@trialx.com.

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts