... admin_list, admin_modify, admin_urls, cache, i18n, l10n, log, static, tz
I have problems with creating a custom tag. I followed the instructions on the official Django documentation. I also copied the code but still get this error.
The file it contains the tag is xxx.py that is in the templatetags dir. Is in app's root directory:
app/
migrations
static
template
templatetags
__init.py__
admin.py
apps.py
models.py
....etc
The tag in xxx.py:
from django import template
import datetime
register = template.Library()
@register.simple_tag
def current_time(format_string):
return datetime.datetime.now().strftime(format_string)
The code in template index.html
{% load xxx %}
{% current_time "%d/%m/%Y %H:%M:%S %p" %}
The traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.1.5
Python Version: 3.9.0
Installed Applications:
['auctions',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template C:\Users\paren\OneDrive\Documenti\Programmazione\copia progetti corso harvard\CS50\project2\commerce\commerce\auctions\templates\auctions\index.html, error at line 1
'xxx' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
i18n
l10n
log
static
tz
1 : {% load xxx %}
2 :
3 :
4 : {% block body %}
5 : {% if message %}
6 : <div class="alert">{{message}}</div>
7 : {% endif %}
8 : <div class="main">
9 : <h2>Active listings</h2>
10 :
11 : {% for ele in msg%}
Traceback (most recent call last):
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\defaulttags.py", line 1034, in find_library
return parser.libraries[name]
During handling of the above exception ('xxx'), another exception occurred:
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\paren\OneDrive\Documenti\Programmazione\copia progetti corso harvard\CS50\project2\commerce\commerce\auctions\views.py", line 17, in index
return render(request,"auctions/index.html",{'msg':qs})
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader.py", line 61, in render_to_string
template = get_template(template_name, using=using)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loader.py", line 15, in get_template
return engine.get_template(template_name)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\backends\django.py", line 34, in get_template
return Template(self.engine.get_template(template_name), self)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\engine.py", line 143, in get_template
template, origin = self.find_template(template_name)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\engine.py", line 125, in find_template
template = loader.get_template(name, skip=skip)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\loaders\base.py", line 29, in get_template
return Template(
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 155, in __init__
self.nodelist = self.compile_nodelist()
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 193, in compile_nodelist
return parser.parse()
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 478, in parse
raise self.error(token, e)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\base.py", line 476, in parse
compiled_result = compile_func(self, token)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\defaulttags.py", line 1091, in load
lib = find_library(parser, name)
File "C:\Users\paren\AppData\Local\Programs\Python\Python39\lib\site-packages\django\template\defaulttags.py", line 1036, in find_library
raise TemplateSyntaxError(
Exception Type: TemplateSyntaxError at /
Exception Value: 'xxx' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
i18n
l10n
log
static
tz
In your project folder name of the template is not in the plural form so, you need to change it to templates because by default Django have this naming convention while creating a project
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.