關於'selffilter' is not a registered tag library. Must be one of:


報錯代碼:

'selffilter' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_static
admin_urls
cache
i18n
l10n
log
static
staticfiles
tz

 解決方法:

 

 

錯誤可能:

1. 在你的app目錄下新建templatetags文件夾里沒創建__init__.py定義為包

 

2. 程序編寫問題

在templatetags下創建myfilter.py 文件

 

html文件中

 

3.settings.py未設置


 上面是摘自https://blog.csdn.net/qq_40326481/article/details/80342480

補充其他的方法:

不知道是我的版本問題還是必須的這么寫,我在檢查了上面的幾種方法之后還是報相同的錯誤,找了好久,終於找到了出錯地地方

原來的代碼:

from django import template
registor = template.Library() #注冊

#告訴Djiango這個函數的名字是mmd
@registor.filter(name="mmd")
def mmd1(arg,name):
    return "%s親了%s一下"%(arg,name)

 

修改:由於我的單詞寫成了registor,所以一直報錯。

把單詞改為register就不報錯了,難道統一了入口?

from django import template
register = template.Library() #注冊

#告訴Djiango這個函數的名字是mmd
@register.filter(name="mmd")
def mmd1(arg,name):
    return "%s親了%s一下"%(arg,name)

 注意在調用函數的時候,

{% load selffilter %}
<p>{{ dict.dxx|mmd:"xpg" }}</p>

 

 一定不要加上空格。

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM