自定义标签(模板标签)未加载

Custom tags (templatetags) not loading

我正在尝试创建一个模板标签,以便找出当前用户属于哪个组。我在另一个项目中有相同的设置,但那个正在工作,但这个没有。这是我的:

项目结构:

- root_directory
-- myproject
--- home
---- templatetags
----- __init__.py
----- custom_tags.py
---- __init__.py
---- urls.py
---- ...
--- myproject
---- settings.py
---- ...

settings.py:

INSTALLED_APPS = [
    .....
    'home.templatetags',
]

custom_tags.py:

from django import template

register = template.Library()


@register.filter
def has_group(user, group_name):
    return user.objects.filter(group_name=group_name).

home_base.html:

{% extends 'navbar.html' %}

{% block content %}

    {% load custom_tags %}

{% endblock %}

回溯:

Internal Server Error: /
Traceback (most recent call last):
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\defaulttags.py", line 1021, in find_library
    return parser.libraries[name]
KeyError: 'custom_tags'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "D:\Projects-2-1\monthlymeeting\home\views.py", line 9, in get
    return render(request, self.template_name, {'entry': 1})
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\backends\django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\loaders\base.py", line 30, in get_template
    contents, origin, origin.template_name, self.engine,
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 156, in __init__
    self.nodelist = self.compile_nodelist()
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 194, in compile_nodelist
    return parser.parse()
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\loader_tags.py", line 266, in do_extends
    nodelist = parser.parse()
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\loader_tags.py", line 209, in do_block
    nodelist = parser.parse(('endblock',))
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\defaulttags.py", line 1078, in load
    lib = find_library(parser, name)
  File "D:\Projects-2-1\monthlymeeting\venv\lib\site-packages\django\template\defaulttags.py", line 1025, in find_library
    name, "\n".join(sorted(parser.libraries)),
django.template.exceptions.TemplateSyntaxError: 'custom_tags' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_static
admin_urls
cache
comments
i18n
l10n
log
static
staticfiles
tz

我试过以下方法:

  1. 重新制作整个项目。
  2. 正在删除所有 .pyc 文件。
  3. 尝试将其导入 settings.py 作为 myproject.home.templatetags 和 home.templatetags.custom_tags。
  4. 正在重启服务器、浏览器、PC。
  5. 我的 init templatetags 中的文件是空的
  6. 我从 PyCharm 中删除了 templatetags 文件夹,从 PyCharm 中将其重新创建为 Python 包(以及目录)。
  7. 从 manage.py shell,我尝试使用从 home.templatetags import custom_tags 导入,它加载时没有任何错误。

同样的结构适用于我的另一个项目。我不知道发生了什么。唯一不同的是,这个项目是Django 2.2.4,另一个是2.2.3。这个项目有一个额外的应用程序 django-contrib-comments.

我一直在努力解决这个问题,我已经用尽了所有可以在网上找到的方法。

您需要{% load %}您的模板中的自定义标签。

  • INSTALLED_APPS 中删除,因为这仅适用于应用程序(但您的应用程序绝对应该在其中,例如 home
  • 使用这些标签在模板顶部添加 {% load custom_tags %}