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

时间:2019-08-26 16:44:22

标签: python django django-apps templatetags

我正在尝试创建一个模板标签,以便找出当前用户所属的组。我在另一个项目中设置了相同的设置,但是该项目正在运行,但是这个项目没有。这就是我所拥有的:

项目结构:

- 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\1-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\1-2-1\monthlymeeting\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "D:\Projects\1-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\1-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\1-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\1-2-1\monthlymeeting\venv\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "D:\Projects\1-2-1\monthlymeeting\home\views.py", line 9, in get
    return render(request, self.template_name, {'entry': 1})
  File "D:\Projects\1-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\1-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\1-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\1-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\1-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\1-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\1-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\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 156, in __init__
    self.nodelist = self.compile_nodelist()
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 194, in compile_nodelist
    return parser.parse()
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\loader_tags.py", line 266, in do_extends
    nodelist = parser.parse()
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\loader_tags.py", line 209, in do_block
    nodelist = parser.parse(('endblock',))
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "D:\Projects\1-2-1\monthlymeeting\venv\lib\site-packages\django\template\defaulttags.py", line 1078, in load
    lib = find_library(parser, name)
  File "D:\Projects\1-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. 试图将其作为myproject.home.templatetags和home.templatetags.custom_tags导入settings.py。
  4. 重新启动服务器,浏览器,PC。
  5. 我的templatetags中的 init 文件为空
  6. 我从PyCharm中删除了templatetags文件夹,并从PyCharm中将其重新创建为Python包(以及目录)。
  7. 我从manage.py shell尝试使用home.templatetags import custom_tags进行导入,并且加载时没有任何错误。

相同的结构正在为我拥有的另一个项目工作。我不知道怎么回事。唯一的区别是该项目是Django 2.2.4,另一个是2.2.3。这个项目还有一个额外的应用django-contrib-comments

我已经尝试修复了几个小时,并且用尽了所有可以在网上找到的方法。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

  • INSTALLED_APPS中删除,因为这仅适用于应用程序
  • 使用这些标记在模板顶部添加{% load custom_tags %}