Laurent
Laurent

Reputation: 1806

django custom filters not working

Here is my folders organization :

enter image description here

core_extras.py content :

from django.contrib.auth.models import Group
from django.conf import settings

from django import template
register = template.Library()

@register.simple_tag
def get_develop_state():
    return settings.DEVELOP

@register.filter(is_safe=True)
def in_group(user, group_name):
    group = Group.objects.get(name=group_name)
    return user.groups.filter(name=group_name).exists()

@register.filter
def do_nothing(value):
    return value

How I load custom tag/filter in html :

{% load i18n staticfiles core_extras %}
{% get_develop_state as DEVELOP %}

How I use my tag/filter :

{% if DEVELOP or request.user|in_group:"testers" %}

The get_develop_state is working correctly, but for my filter in_group, I got the error Invalid filter: 'in_group'.

The application core is in my INSTALLED_APPS.

Why is my filter not registered ?

Upvotes: 0

Views: 1597

Answers (1)

Laurent
Laurent

Reputation: 1806

I'm stupid.

I loaded {% load i18n staticfiles core_extras %} in a base html file which I extend then on child html files. The get_develop_state was working because I import it as a variable DEVELOP which is available on the child html files but not the filter in_group. I just loaded {% load in_group %} in the child html and everything works...

Upvotes: 3

Related Questions