我如何面对无效的过滤器:自定义Django模板标签中的“ has_group”错误

时间:2019-01-28 08:32:40

标签: django templatetags

尽管我已经创建了 templatetags 文件夹并将__init__.py放在下面的代码中,

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

我仍然遇到错误

Invalid filter: 'has_group'

我想基于我在admin中创建的特定组来授予对某些功能的访问权限。

这是我的模板的示例。

{% 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 %}

Traceback堆栈使我的views.py

中的 return 代码行出错。
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)

我想念什么?

在这里追溯

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'

1 个答案:

答案 0 :(得分:2)

将您的代码放入已注册应用程序的文件templatetags/has_group.py中。确保templatetags还包含一个__init__.py文件。

然后将{% load has_group %}添加到模板中。