[英]TemplateSyntaxError at / 'xxx' is not a registered tag library. Must be one of:
[英]'base_tags' is not a registered tag library. Must be one of:
您好,我在 django 中創建新的自定義模板標簽時遇到此錯誤,我該如何調試我的代碼?
這是我的模板標簽:
from django import template
from ..models import Category
register = template.Library()
@register.simple_tag
def title():
return "any thing"
這是我的 HTML 代碼:
{% load base_tags%}
<a class="navbar-brand" href="index.html">{% title %}</a>
這是我的錯誤:'base_tags' 不是注冊的標簽庫。 必須是以下之一: admin_list admin_modify admin_urls cache i18n l10n log static tz
我在終端中運行網絡服務器,但它再次不起作用
你得到這個錯誤是因為你沒有在 settings.py 文件中添加這個標簽
改變這個:
@register.simple_tag
對此:
@register.simple
在模板中:
{% load simple_tags %}
在 settings.py 文件中:
'libraries':{
'simple_tags': 'templatetags.simple',
}
如果應用程序中有 templatetags 目錄,則必須在庫中添加 appname
'libraries':{
'filter_tags': 'your_appname.templatetags.simple',
}
前任:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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':{
'simple_tags': 'your_appname.templatetags.simple', #Added here
}
},
},
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.