zmb*_*mbq 4 python django filter django-templates
我正在尝试在我的Django应用程序中创建自定义模板过滤器.我在我的应用程序下创建了一个templatetags文件夹,在__init__.py我的过滤器中添加了一个文件和一个filters.py文件,如下所示:
import os
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def filename(pathname):
""" Returns the file's name, without folder """
return os.path.basename(pathname)
Run Code Online (Sandbox Code Playgroud)
当我尝试从模板访问过滤器时,出现错误"无效过滤器'文件名'"
这个问题已经被问了很多,所以我已经检查了以下内容:
__init__.py 正确命名manage.py shell我可以导入我的过滤器,如下所示:import app.templatetagstemplate.library()- 调试器并没有就此止步.这是模板(我删除了它的一部分,但错误仍然存在于这个小版本中)
<form enctype="multipart/form-data" action="{% url lab.views.new.artefact_drop_web tempfile_id=tempfile.id %}" method="post">
{% csrf_token %}
<h3>File:</h3>
<p>{{ tempfile.file.name|filename }}</p>
<input type="submit" value="Add" />
</form>
Run Code Online (Sandbox Code Playgroud)
Nge*_*tor 11
您需要{% load name_of_file_tags_are_in %}在模板中使用.
https://docs.djangoproject.com/en/1.5/howto/custom-template-tags/