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

﹏ヽ暗。殇╰゛Y 2022-01-22 22:39 242阅读 0赞

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

解决方案:

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

如下所示:

  1. TEMPLATES = [
  2. {
  3. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  4. 'DIRS': [os.path.join(BASE_DIR, 'templates')]
  5. ,
  6. 'APP_DIRS': True,
  7. 'OPTIONS': {
  8. 'context_processors': [
  9. 'django.template.context_processors.debug',
  10. 'django.template.context_processors.request',
  11. 'django.contrib.auth.context_processors.auth',
  12. 'django.contrib.messages.context_processors.messages',
  13. ],
  14. 'libraries':{
  15. "filters_2":"booktest.templatetags.filters_2",
  16. },
  17. },
  18. },
  19. ]

疑问:

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

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

发表评论

表情:
评论列表 (有 0 条评论,242人围观)

还没有评论,来说两句吧...

相关阅读