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



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

项目结构:

- 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:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatedefaulttags.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:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangocorehandlersexception.py", line 34, in inner
response = get_response(request)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangocorehandlersbase.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangocorehandlersbase.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangoviewsgenericbase.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangoviewsgenericbase.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "D:Projects1-2-1monthlymeetinghomeviews.py", line 9, in get
return render(request, self.template_name, {'entry': 1})
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangoshortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplateloader.py", line 61, in render_to_string
template = get_template(template_name, using=using)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplateloader.py", line 15, in get_template
return engine.get_template(template_name)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebackendsdjango.py", line 34, in get_template
return Template(self.engine.get_template(template_name), self)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplateengine.py", line 143, in get_template
template, origin = self.find_template(template_name)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplateengine.py", line 125, in find_template
template = loader.get_template(name, skip=skip)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplateloadersbase.py", line 30, in get_template
contents, origin, origin.template_name, self.engine,
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebase.py", line 156, in __init__
self.nodelist = self.compile_nodelist()
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebase.py", line 194, in compile_nodelist
return parser.parse()
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebase.py", line 478, in parse
raise self.error(token, e)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebase.py", line 476, in parse
compiled_result = compile_func(self, token)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplateloader_tags.py", line 266, in do_extends
nodelist = parser.parse()
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebase.py", line 478, in parse
raise self.error(token, e)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebase.py", line 476, in parse
compiled_result = compile_func(self, token)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplateloader_tags.py", line 209, in do_block
nodelist = parser.parse(('endblock',))
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebase.py", line 478, in parse
raise self.error(token, e)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatebase.py", line 476, in parse
compiled_result = compile_func(self, token)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatedefaulttags.py", line 1078, in load
lib = find_library(parser, name)
File "D:Projects1-2-1monthlymeetingvenvlibsite-packagesdjangotemplatedefaulttags.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. 模板标签内的初始化文件为空
  6. 我从 PyCharm 中删除了 templatetags 文件夹,从 PyCharm 将其重新创建为 Python 包(以及目录(。
  7. 从 manage.py shell 中,我尝试使用 fromhome.templatetags import custom_tags导入,它加载时没有任何错误。

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

我一直在尝试解决这个问题,我已经用尽了我在网上能找到的所有方法。

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

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

相关内容

  • 没有找到相关文章

最新更新