So I was just working on a project and wanted to create my own template filter to help me do stuffs. And I looked at Django's documentation, on how to create a custom template filters. I followed all the instructions but too bad it didn't work :( and gave me the TemplateSyntaxError at my_app/ and also with Invalid Filter: 'my_filter'

Then I looked on the internet and read some answers from stack overflow but still couldn't get things to work. In the end, I found a fix to the problem.

In addition to create a templatetags directory inside my app, I also need to update the TEMPLATES inside the project's settings.py. So now, I will hopefully add all the steps needed to create your own template filter.

First, you need to create the templatetags directory inside my_app. So the structure will look something like this

my_app
|- models.py
|- views.py
|- templatetags
|- __init__.py
|- app_filters.py

And then you put your custom filter inside app_filters.py

from django import template
register = template.Library()

@register.filter(name='my_filter')
def my_filter(ob):
...

And also update settings.py

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'builtins': [
'my_app.templatetags.app_filters',
],
...

Also, don't forget to load it inside template.html

{% load app_filters %}

I hope this will help people who had this problem. Have a nice day :)