我正在使用Django 2.0
.
我已经编写了一些自定义模板标签以在
应用程序目录notes/templatetags/note_tags.py
所在的文件内的模板中使用notes
我在这个文件中写了几个自定义标签
from django import template
from django.template.defaultfilters import stringfilter
from notepad.utils import simplify_text
from notes.models import Note, ColorLabels
register = template.Library()
@register.filter(name='note_simplify')
@stringfilter
def note_simplify(value):
return simplify_text(value)
@register.filter(name='default_color_label')
def default_color_label():
default_label = ColorLabels.objects.filter(default=True).first()
print(default_label.value)
return default_label
在template
文件中,我已将标签加载为
{% load note_tags %}
我可以使用第一个标签note_simplify
,但default_color_label
没有调用第二个标签。我在同一个文件中使用这两个标签。一个用于修改传递的数据,另一个用于简单地打印一些东西
# modify the note.content
<p>{{ note.content|truncatechars:200|note_simplify }}</p>
# to print some value
{{ default_color_label.value }}
我也多次重启服务器。
有什么不对?为什么在模板中没有调用标签?