当前位置: 代码迷 >> 综合 >> 【2019.06.01】python3.7+ Django2.2自定义过滤器报错 xxx is not a registered tag library.
  详细解决方案

【2019.06.01】python3.7+ Django2.2自定义过滤器报错 xxx is not a registered tag library.

热度:63   发布时间:2023-12-22 22:46:02.0

在django 2.x版本有这个问题,应该是Django2.0以上自定义的过滤器需要在配置文件setting.py中配置后才会生效。

解决方案:

你只需要在setting.py 的 模板配置中添加如下代码
'libraries':{ "filters_2":"booktest.templatetags.filters_2", },

如下所示:

TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR, 'templates')],'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',],'libraries':{"filters_2":"booktest.templatetags.filters_2",},},},
]

疑问:

在settings 的 TEMPLATES 添加上libraries 键值对
'libraries':{ "filters_2":"booktest.templatetags.filters_2", },能显示出过滤后的数据了;但当注释后,xxx is not a registered tag library. 这个错误也不再出现了,有点奇怪。

如果有帮到你,点个赞呗。

  相关解决方案