【发布时间】:2018-06-07 10:14:02
【问题描述】:
我正在使用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 }}
我也多次重启服务器。
有什么问题吗?为什么模板中没有调用标签?
【问题讨论】:
标签: django django-template-filters django-2.0