Search code examples
djangotemplatetags

How can I face Invalid filter: 'has_group' error in custom django templatetags


Despite the fact that I have created a templatetags folder and put in __init__.py the code below,

from django import template
from django.contrib.auth.models import Group 

register = template.Library()

@register.filter(name='has_group')
def has_group(user, group_name): 
    group = Group.objects.get(name=group_name) 
    return True if group in user.groups.all() else False

I still facing the error

Invalid filter: 'has_group'.

I want based on a specific group that I have created in admin to give access to certain functions.

This is an example of my template.

{% if request.user|has_group:"operationalusers" %}
<div class="col-md-12">
<h1>Warehouse</h1>
    <div style="margin: 0 auto; text-align:center; padding:1em;">
    <a href="{% url 'warehouse_stuffing_new' %}">
     <button class="btn btn-success" type="">New Entry</button></a>
    <a href="{% url 'upload_csv' %}">
    <button class="btn btn-success" type="">Bulk Entry</button></a>
    </div>
    {% endif %}

The Traceback stack gives an error to the return line of code in my views.py

def warehouse_stuffing_list(request, template_name='warehouse/warehouse_list.html'):
    products_with_serial_numbers = ProductSerialNumbers.objects.all()
    data = {}
    data['object_list'] = products_with_serial_numbers
    return render(request, template_name, data)

What I miss?

Here the traceback

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/vhosts/intranet.health-nutrition.gr/farmakeio/intranet/views.py", line 1938, in warehouse_stuffing_list
    return render(request, template_name, data)
  File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 67, in render_to_string
    template = get_template(template_name, using=using)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 21, in get_template
    return engine.get_template(template_name)
  File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py", line 39, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "/usr/local/lib/python2.7/dist-packages/django/template/engine.py", line 162, in get_template
    template, origin = self.find_template(template_name)
  File "/usr/local/lib/python2.7/dist-packages/django/template/engine.py", line 136, in find_template
    name, template_dirs=dirs, skip=skip,
  File "/usr/local/lib/python2.7/dist-packages/django/template/loaders/base.py", line 44, in get_template
    contents, origin, origin.template_name, self.engine,
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 191, in __init__
    self.nodelist = self.compile_nodelist()
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 230, in compile_nodelist
    return parser.parse()
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 515, in parse
    raise self.error(token, e)
TemplateSyntaxError: Invalid filter: 'has_group'

Solution

  • Put your code in a file templatetags/has_group.py in a registered app. Make sure templatetags also contains an __init__.py file.

    Then add {% load has_group %} to your template.