Django's template custom filter does not work. "Invalid Filter"

538 Views Asked by At
| app_name
    | __init__.py
    | models.py
    | templatetags
         |__init__.py
         |exp_tags.py

exp_tags.py

from django import template

register = template.Library()

@register.filter
def percentage_of(numerator, denominator):
    try:
        return "%.2f%%" % (float(numerator) / float(denominator) * 100)
    except (TypeError, ValueError, ZeroDivisionError):
        return ""

The view.py is from another app within the same project. All these apps are registered at INSTALLED_APPS in settings.py

template.html

{% load exp_tags %}
{{ variable1|percentage_of:variable2 }}

PyCharm is not being able to resolve the reference to exp_tags too.

I'm getting this error on the browser.

TemplateSyntaxError at /...../...../...../

Invalid filter: 'percentage_of'

This is a really minor error which has been eating up my time considerably. Any help would be appreciated. Thanks.

0

There are 0 best solutions below