Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django registration of tag library not working

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.

like image 731
fredo.r Avatar asked May 30 '17 14:05

fredo.r


People also ask

What is tag in Django?

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 »

What are templates in Django?

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.


2 Answers

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

like image 118
Santiago de Pedro Avatar answered Oct 04 '22 04:10

Santiago de Pedro


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.

like image 22
saleh afzoon Avatar answered Oct 04 '22 03:10

saleh afzoon