Django:模板没有加载app_tags文件。无法使用自定义过滤器:"无效过滤器"

时间:2016-09-21 15:53:17

标签: django django-templates django-template-filters

我有以下文件夹结构:

myProject
        |
       myapp
           |
          templatetags
                     | __init__.py
                     | app_tags.py

app_tags.py文件:

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter(is_safe=False)
@stringfilter
def upper2(value):
    """Converts a string into all uppercase."""
    return value.upper()

test.html模板:

{% load app_tags % }

<div>Test Word: {{ test_word }}</div>
<div>Test Word: {{ test_word|upper2 }}</div>

如果我使用{{ test_word|upper2 }},我会收到Invalid filter: 'upper2'错误。

enter image description here

如果我不尝试使用upper2过滤器,则{% load app_tags % }似乎无法加载。

enter image description here

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

并且...... {% load app_tags % }

中有一个空白区域

我将加载代码更改为{% load app_tags %}后,效果很好!

相关问题