Django中添加自定义的simple_tag

Django自定义simple_tag时出错如下:

TemplateSyntaxError at /app/

'my_tag' 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
xxoo

解决办法:
  在setting.py文件中添加注红部分
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'libraries':{
"你的自定义标签":"app名.templatetags.自定义函数",
},

},
},
]

注意:

html模板中
{% load 文件名 %} 对应 templatetags中的 py文件名



{% 自定义标签 args %} 对应templatetags下py文件中函数


原文地址:http://blog.51cto.com/zhangrf/2107991


努力有用的话,还要天才做什么呢?
原文地址:https://www.cnblogs.com/crazy-xf/p/9309339.html