【发布时间】:2011-08-02 01:26:33
【问题描述】:
我有一个文章应用程序并尝试制作一个自定义过滤器,我在文章应用程序中有一个名为 templatetags 的目录,该目录中有一个 tags.py,这是目录结构。
-manage.py(f)
-settings.py(f)
-articles(d)
- templatetags(d)
- tags.py(f)
在模板上,文章有自己的目录,所有文章模板都是从一个base.html模板扩展而来,这里是模板结构。
-base.html(f)
-articles(d)
-index.html(f)
我在 base.html {% load tags %} 中加载标签,并在 index.html 中使用自定义过滤器并得到无效过滤器错误。
tags.py
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def space2Dash(s):
return s.replace(' ', '_');
我就是想不通我做错了什么。
编辑:
我将过滤器名称更改为abcfilter.py,并在我的settings.py 中加载了文章应用程序
articles/index.html
{% load abcfilter %}
{{ "foo bar"|space2dash }}
错误:
Request Method: GET
Request URL: http://localhost:8080/articles/
Django Version: 1.2.5
Exception Type: TemplateSyntaxError
Exception Value:
Invalid filter: 'space2dash'
Exception Location: ***/lib/python2.7/django/template/__init__.py in find_filter, line 363
Python Executable: /usr/local/bin/python
Python Version: 2.7.1
Server time: Sun, 10 Apr 2011 07:55:54 -0500
【问题讨论】:
标签: django django-template-filters