Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django : 'tag' is not a registered tag library error

I'm having this error when loading a custom tag in my template. I've visited many topics about this and I made sure to verify that I didn't commit some common errors :

  • The file containing the tags is in the templatetags folder.
  • This templatetags folder contains a __init__.py file.
  • The app actualites is in the INSTALLED_APPS list from the settings.
  • I'm using {% load mes_tags %} at the beginning of my template.

Here is the file structure of my app :

actualites/
    __init__.py
    SOME FILES
    templatetags/
        __init__.py
        mes_tags.py

mes_tags.py

from django import template

register = template.Library()

@register.simple_tag(takes_context=True)
def param_replace(context, **kwargs):
    d = context['request'].GET.copy()
    for k, v in kwargs.items():
        d[k] = v
    for k in [k for k, v in d.items() if not v]:
        del d[k]
    return d.urlencode()

The error I get is the following :

TemplateSyntaxError at /
'mes_tags' is not a registered tag library. Must be one of:
LIST OF TAGS

Can someone tell me what I did wrong ? Thanks in advance !

like image 915
Julien Mertz Avatar asked Jan 25 '23 09:01

Julien Mertz


1 Answers

You need to add this tags library in settings (for Django >= 1.9):

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    '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',
            'app.apptemplates.load_setting',

        ],

        'libraries':{
            'custom_templatetag': 'actualites.templatetags.mes_tags',

            }
    },
}]

You can read more here

like image 75
Charnel Avatar answered Jan 31 '23 08:01

Charnel