当做{% load custom_filters %}
在模板中,之后{% extends "base.html" %}
一切工作正常,但是当我将负载移动到 base.html 模板时,过滤器会出现奇怪的行为。这是我的custom_filters.py
:
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
# To cut off strings at a specified character, at first occurance. Example:
# time = 19:30:12.123456
# {{ time|cut:'.' }}
# returns: 19:30:12
@register.filter
@stringfilter
def cut(string, cutoff_point):
return string.split(cutoff_point, 1)[0]
当我将其加载到“最终模板”中时,行为符合预期。如果time = 19:30:12.123456
then {{ time|cut:'.' }}
回报19:30:12
。当我加载它时base.html
返回值是19:30:12123456
,与输入相同,但没有“截止点”。
有谁知道为什么?
你应该放置{% load ... %}
在每个模板中,您想要使用自定义标签或过滤器。
在你的情况下,调用过滤器也不是一个好主意cut
,因为这个过滤器已经存在 https://docs.djangoproject.com/en/dev/ref/templates/builtins/#cut(它用于从字符串上切下一个点)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)