我試圖在我的Django應用程序中創建自定義模板過濾器。我已經在我的應用程序下創建創建一個templatetags文件夾,添加一個__init__.py
文件,並與我的過濾器filters.py文件,像這樣:Django無法找到我的自定義模板過濾器
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)
當我嘗試從模板訪問過濾器,我得到的錯誤「無效的過濾器‘名’」
這個問題已經被問了很多,所以我已經檢查以下內容:
__init__.py
正確命名爲- 與打開後殼我可以導入我的過濾器,像這樣:
import app.templatetags
- 我也可以正常使用過濾器從Django的殼
- 我已經重新啓動Django開發服務器。
- 我在調用
template.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>
你能發佈你的模板嗎? – 2013-04-04 08:40:57
已發佈。這當然是減少了模板的版本。 – zmbq 2013-04-04 08:48:29
我的疑惑是在創建templatetag文件後手動重新啓動django服務器。偉大的事情總結檢查! – scum 2015-06-10 16:56:20