Django : 'tag' 不是注册的标签库错误
Django : 'tag' is not a registered tag library error
我在模板中加载自定义标签时遇到此错误。我已经访问过许多关于此的主题,并且确保验证我没有犯一些常见错误:
- 包含标签的文件在
templatetags
文件夹中。
- 此
templatetags
文件夹包含一个 __init__.py
文件。
- 应用
actualites
在设置的 INSTALLED_APPS
列表中。
- 我在模板的开头使用
{% load mes_tags %}
。
这是我的应用程序的文件结构:
actualites/
__init__.py
SOME FILES
templatetags/
__init__.py
mes_tags.py
mes_tags.py
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def param_replace(context, **kwargs):
d = context['request'].GET.copy()
for k, v in kwargs.items():
d[k] = v
for k in [k for k, v in d.items() if not v]:
del d[k]
return d.urlencode()
我得到的错误如下:
TemplateSyntaxError at /
'mes_tags' is not a registered tag library. Must be one of:
LIST OF TAGS
谁能告诉我我做错了什么?
提前致谢!
您需要在设置中添加此标签库(对于 Django >= 1.9):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'app.apptemplates.load_setting',
],
'libraries':{
'custom_templatetag': 'actualites.templatetags.mes_tags',
}
},
}]
你可以阅读更多here
我在模板中加载自定义标签时遇到此错误。我已经访问过许多关于此的主题,并且确保验证我没有犯一些常见错误:
- 包含标签的文件在
templatetags
文件夹中。 - 此
templatetags
文件夹包含一个__init__.py
文件。 - 应用
actualites
在设置的INSTALLED_APPS
列表中。 - 我在模板的开头使用
{% load mes_tags %}
。
这是我的应用程序的文件结构:
actualites/
__init__.py
SOME FILES
templatetags/
__init__.py
mes_tags.py
mes_tags.py
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def param_replace(context, **kwargs):
d = context['request'].GET.copy()
for k, v in kwargs.items():
d[k] = v
for k in [k for k, v in d.items() if not v]:
del d[k]
return d.urlencode()
我得到的错误如下:
TemplateSyntaxError at /
'mes_tags' is not a registered tag library. Must be one of:
LIST OF TAGS
谁能告诉我我做错了什么? 提前致谢!
您需要在设置中添加此标签库(对于 Django >= 1.9):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'app.apptemplates.load_setting',
],
'libraries':{
'custom_templatetag': 'actualites.templatetags.mes_tags',
}
},
}]
你可以阅读更多here