How to Create Custom Template Tags and Filters in Django?

Published On: 24/12/2022 | Category: Django

Hi Dev,

Are you looking for example of how to create custom template tags and filters in django. We will look at example of how to create custom template tags in django. you can see how to create custom template filters in django. if you want to see example of django create custom template tag example then you are a right place.

In Django Template Tags, which are straightforward Python functions that accept arguments and, after processing, return the values that can be displayed in templates, to construct custom tags and filters that are accessible to templates. HTML or templates both utilise these tags. The template engine can be expanded by creating unique tags and filters.

The Django App is the most popular location for defining template tags. Create an app with the name core, then include it in INSTALLED_APPS. Make a templatetags folder in the core app's and files. To ensure that the directory is considered as a Python package, create the file within the templatetags folder.

Here i will give you we will help you to give example of how to create custom template tags and filters in django. So let's see the bellow example:

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2: Create a App
cd example
django-admin startapp core

Step 3: Update

Then update INSTALLED_APPS within our file to notify Django about the app, in file so our setting file look like this..


Now looks like our app directory structure..

├── core
│ ├──
│ ├──
│ ├──
│ ├──
│ ├── templates
│ │ ├── index.html
│ │ └── users.html
│ ├── templatetags
│ │ ├──
│ │ ├──
│ ├──
│ └──

Step 4: Creating the Views

In this step, we need to configure views. open the core/ file and add:

from django.shortcuts import render

# Create your views here.

def index(request):
    context = {
        "author": "bhavesh sonagra",
    return render(request, "index.html", context)  

Step 5: Creating Template Filters

The only thing that filters are is a Python function with one or two arguments. For instance, the filter filter f would be provided to the variable var and argument as arg in the filter {{var|filter_f:arg}}.


Simple tag accepts a number of arguments and processes them before returning a result. The date and time can be displayed in the template using the straightforward example below.


Common template tags like inclusion tag assist in displaying data by producing another template. This element comes in handy when you need to render data that is shared across multiple pages.

from django import template

register = template.Library()

# Writing Custom Template Filters

def modify_name(value, arg):
    # if arg is first_name: return the first string before space
    if arg == "first_name":
        return value.split(" ")[0]
    # if arg is last_name: return the last string before space
    if arg == "last_name":
        return value.split(" ")[-1]
    # if arg is title_case: return the title case of the string
    if arg == "title_case":
        return value.title()
    return value
register.filter('modify_name', modify_name)

# Writing Custom Template Tags

import datetime

def current_time(format_string):

# Inclusion Tag:

from django.contrib.auth import get_user_model
from django.template.loader import get_template

User = get_user_model()

def show_users_table():
    users = User.objects.all()
    return {'users': users}

users_template = get_template('users.html')

Step 6: Creating the Templates

Next, then with your text editor create new templates files: core/templates/index.html file and the add:

{% load custom_tags %}
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <style type="text/css">
            background-color: #f7fcff;
    <div class="container mt-5 pt-2">
        <div class="row d-flex justify-content-center">
            <div class="col-md-10">
                <div class="card">
                    <div class="card-header">
                        <h4>How to Create Custom Template Tags and Filters in Django? - <span class="text-primary"></span></h4>
                    <div class="card-body">
                        <p>{{ author | modify_name:"first_name"}}</p>
                        <p>{{ author | modify_name:"last_name"}}</p>
                        <p>{{ author | modify_name:"title_case"}}</p>
                        <p>{{ author | modify_name:"first_name" | modify_name:"title_case"}}</p>


                        <p>{% current_time "%d/%m/%Y %H:%M:%S %p" %}</p>


                        <p>{% show_users_table %}</p>
{% load custom_tags %}

<table class="table table-bordered">
            <th>First name</th>
            <th>Last name</th>
        {% for user in users %}
        {% endfor %}

Step 7: Creating URLs

In this section, we need a file within the core app however Django doesn't create one for us with the startapp command. Create core/ with your text editor and paste below code.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),

Next, we require to add a URL path for our example project which can be done by importing include and setting a path for it.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls')),

I hope it will help you....

Happy Coding!