Hello I get this error for create new custom template tags in django how i can debug my code?
it's my template tags:
from django import template
from ..models import Category
register = template.Library()
@register.simple_tag
def title():
return "any thing"
it's my HTML code:
{% load base_tags%}
<a class="navbar-brand" href="index.html">{% title %}</a>
it's my error: 'base_tags' is not a registered tag library. Must be one of: admin_list admin_modify admin_urls cache i18n l10n log static tz
I run webserver in terminal but for again it's doesn't work true
You got this error because you have not added this tag in settings.py file
change this:
@register.simple_tag
To this:
@register.simple
And in templates:
{% load simple_tags %}
And in settings.py file:
'libraries':{
'simple_tags': 'templatetags.simple',
}
If templatetags directory inside an app then you must add appname in libraries
'libraries':{
'filter_tags': 'your_appname.templatetags.simple',
}
EX:
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
}
},
},
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.