[Solved]-Django registration of tag library not working

21👍

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.

9👍

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

2👍

Just for a reminder, when using django under Windows, it is necessary to restart the development server (python.exe manage.py runserver) in at least two situations, which are:

  • when a new templatetag was created in an app
  • when static files were modified in the ‘app/static/app/’ folder

Hope this helps

1👍

I faced the same problem, but reloading the server doesn’t work.
So I solved it by this:

at project/setting.py I wrote the next, at TEMPLATE I registered libraries:

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': {
            'my_tags': 'app.templatetags.blog_tags',
        }
    },
},

]

And the server restarted by itself.

0👍

In Django 3.1 i solved it putting my py file inside templatetags folder inside my app. As the docs said. custom template tags.
The py file must have register = template.Library()

myapp/templatetags/mytags.py

in settings i have myapp inside INSTALLED_APPS array

Then in my template file

{% load mytags %}

Leave a comment