Making your own custom filter tags in Django


Filters are really very helpful, there are many built-in filters in Django. We can also make our own filters in Django which we can use in Django projects in any template file. Filters are used in many ways like making lowercase to uppercase in HTML.

In this article, we will see how to make a custom template filter tag in Django. So, let's get started.

Example

First of all, create a Django project and an app.

Add a templates and templatetags directory in your apps root project. The project structure would look like this −

example is our app and tutorial is our project.

Make sure your TEMPLATES variable in settings.py looks like this −

import os
TEMPLATES = [
   {
      'BACKEND': 'django.template.backends.django.DjangoTem
plates',
      'DIRS': [os.path.join(BASE_DIR, 'example/templates')]
,
      'APP_DIRS': True,
      'OPTIONS': {
         'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
         ],
      },
   },
]

Add "example" to INSTALLED_APPS variable of settings.py. It just sets up the templates folder.

In the templates folder, add a home.html and leave it like that for now.

In views.py of example, add the following lines −

from django.shortcuts import render

# Create your views here.
def main(request):
   return render(request,"home.html")

Here we simply rendered our main frontend in main view.

In urls.py of the project, add the following lines −

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
   path('',include('example.urls')),
   path('admin/', admin.site.urls),
]

Here we included our apps urls.py in main urls.

In urls.py of example or app, add the following −

from django.urls import path,include
from . import views
urlpatterns = [
   path('', views.main,name="main")
]

It sets up the basic path urls and render our main view.

Create __init__.py in templatetags folder and a filter file named "upperfilter.py". Add the following code in it −

from django.template import Library

register=Library()

@register.filter
def upper(value):
   return value.upper()

Here we imported a Library and registered it. We created a function or we can say a filter named upper and we registered it using decorator. Each filter takes some value; we can define how many values it can take.

Come back to your home.html and add −

<!DOCTYPE html>
<html>
   <head>
      <title>Tut</title>
   </head>
   <body>
      {% load upperfilter %}
      <h1>{{"hi friends how are you" | upper}}</h1>
   </body>
</html>

Here on the frontend, we loaded our filter using {% %}, and then in <h1> element, we used the filter.

This filter will convert lowercase to uppercase.

Output

Without filter

With filter

Updated on: 26-Aug-2021

244 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements