【问题标题】:Django: Template isn't loading app_tags file. Unable to use custom filter: "Invalid Filter"Django:模板未加载 app_tags 文件。无法使用自定义过滤器:“无效过滤器”
【发布时间】:2017-01-29 23:35:05
【问题描述】:

我有以下文件夹结构:

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' 错误。

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

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    而且...{% load app_tags % } 中有一个空格

    将加载标签更改为 {% load app_tags %} 后,效果很好!

    【讨论】:

      猜你喜欢
      • 2017-02-10
      • 2018-03-15
      • 2015-11-08
      • 1970-01-01
      • 2011-05-23
      • 2018-07-28
      • 1970-01-01
      • 2011-08-02
      • 2011-05-28
      相关资源
      最近更新 更多