I try to register my custom template tag library in django, but unfortunately it isnt working!
I want to create a custom include-tag and followed the instruction at: https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#howto-custom-template-tags-inclusion-tags
I have created an app called 'tag_lib' (installed in settings.py) to put my template tags in. In the app folder is a dictionary called 'templatetags' containing an empty __init__.py
and my my_tags.py
.
my_tags.py
contains:
from django import template
register = template.Library()
@register.inclusion_tag(filename='navbar.html', takes_context=True)
def navbar_context(context):
return {
'some_var': context['some_var'],
}
When I restart my devserver and try to load the library with
{% load my_tags %} `
in a template, I'm getting the error:`
TemplateSyntaxError at /
'my_tags' 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
Any idea where I made a mistake?
Thanks.
Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client. template.html : <ul> {% for x in mymembers %} <li>{{ x. firstname }}</li> {% endfor %} </ul> Run Example »
A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.
According to django documentation:
Development server won’t automatically restart after adding the templatetags module, you will need to restart your server before you can use the tags or filters in templates.
So, to resolve the issue, you need restart development server.
Source: https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/#code-layout
I faced this problem to.what i did was just stop the server run and just run it again.It seems that django does not initialize tags (or resources in general) while running the server.hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With