The Django template language offers a wide range of built-in tags and filters for designing the presentation layer of your Django app. However, your app may need a functionality that is not included in the core set of template primitive language. Django allows you to create your own set of custom tags and filters using Python, and make them available in the Django templates.
This guide will demonstrate how to create custom template tags and filters, and how to load them in templates.
Inside your Django app directory, create a module called templatetags
and add an empty __init__.py
file as shown in the below directory structure.
Note: Make sure that you have included your app in
INSTALLED_APPS
available insettings.py
.
1my_app/
2├── __init__.py
3├── admin.py
4├── models.py
5├── templatetags/
6│ ├── __init__.py
7│ └── custom_tags.py
8└── views.py
Next, open the custom_tags.py
file and add these two lines to get started with custom template tags and filters.
1from django import template
2
3register = template.Library()
Make custom_tags
available by loading it in templates.
1{% load custom_tags %}
Filters are nothing but a Python function, which takes one or two arguments. For instance, in the filter {{var|filter_f:arg}}
, the filter filter_f
would be passed to the variable var
and argument as arg
.
For demonstration, create a filter to modify a string based on the argument. Create a simple view in views.py
, that renders a string as follows:
1def my_view(request):
2 context = {
3 "author": "gaurav singhal",
4 }
5 return render(request, "index.html", context)
After creating views, create a simple template filter named modify_name
in custom_tags.py
.
1from django import template
2
3register = template.Library()
4
5def modify_name(value, arg):
6 # if arg is first_name: return the first string before space
7 if arg == "first_name":
8 return value.split(" ")[0]
9 # if arg is last_name: return the last string before space
10 if arg == "last_name":
11 return value.split(" ")[-1]
12 # if arg is title_case: return the title case of the string
13 if arg == "title_case":
14 return value.title()
15 return value
16
17register.filter('modify_name', modify_name)
You can now use the modify_name
filter in your templates.
1{% comment %} index.html {% endcomment %}
2
3{% load custom_tags %}
4
5{{ author | modify_name:"first_name"}}<br>
6{{ author | modify_name:"last_name"}}<br>
7{{ author | modify_name:"title_case"}}<br>
8{{ author | modify_name:"first_name" | modify_name:"title_case"}}<br>
This will give the output as follows:
1gaurav
2singhal
3Gaurav Singhal
4Gaurav
Likewise, you can create your template filters, and then you can use them in your templates.
Django custom template tags and filters are the best way to create tags and filters that you may need in your Django app. If you are new to Django templates, read this guide. You can refer to the Django documentation on custom template tags and filters for more understanding. If you have any queries, feel free to reach out at Codealphabet.