[英]Invalid filter error in django custom template filter
I'm trying to create a custom template filter but, getting Invalid filter
error. 我正在尝试创建自定义模板过滤器,但收到
Invalid filter
错误。
TemplateSyntaxError at /attainment/reportcard/
Invalid filter: 'get_item'
Request Method: POST
Request URL: http://127.0.0.1:8000/attainment/reportcard/
Django Version: 1.11.5
Exception Type: TemplateSyntaxError
Exception Value:
Invalid filter: 'get_item'
Exception Location: /media/itsd/ITSD/ROFI/Projects/Rise/venv/lib/python3.5/site-packages/django/template/base.py in find_filter, line 606
Python Executable: /media/itsd/ITSD/ROFI/Projects/Rise/venv/bin/python
Python Version: 3.5.2
I've created a filter in template_filters.py
and registered. 我已经在
template_filters.py
创建了一个过滤器并进行了注册。
template_filters.py template_filters.py
from django.template.defaulttags import register
# Custom template filter to get data from a dictionary using key in template
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
And I've used the filter in the template as follows. 我在模板中使用了过滤器,如下所示。
<tbody>
{% for subject in subjects %}
<tr>
<td>{{ subject|get_item:"subject" }}</td>
<td>{{ subject|get_item:"efffort" }}</td>
<td>{{ subject|get_item:"homework" }}</td>
<td>{{ subject|get_item:"marks" }}</td>
<td>{{ subject|get_item:"grade" }}</td>
</tr>
{% endfor %}
</tbody>
What am I missing ?? 我想念什么??
You have to load the tag library before you use it in the template: 您必须先加载标签库,然后才能在模板中使用它:
{% load template_filters %}
Or you can load the specific filter: 或者,您可以加载特定的过滤器:
{% load get_item from template_filters %}
See the docs on custom template tags and the {% load %}
tag for more info. 有关更多信息,请参阅有关自定义模板标签和
{% load %}
标签的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.