在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. 这个错误也不再出现了,有点奇怪。