[Fixed]-Register custom filter in django

24👍

First of all, you haven’t defined register:

To be a valid tag library, the module must contain a module-level
variable named register that is a template.Library instance, in which
all the tags and filters are registered.

Also, I usually decorate the function with register.filter:

from django import template

register = template.Library()

@register.filter
def intcomma(value):
    return value + 1
👤alecxe

2👍

import register from django.template.defaulttags

from django.template.defaulttags import register

views.py

        from django.template.defaulttags import register
    
    @register.filter
    def intcomma(value):
        return value + 1
..........................other code.................................

enter image description here

list.html

{% extends "base.html" %}

    {% block content %}
    
      <ul>
        <li><h1>{{ 2.568|intcomma|floatformat:"0" }}</h1></li>
      </ul>
    
    {% endblock content %}

enter image description here

Output like this:-

enter image description here

👤lava

Leave a comment