How to Solve Error Message TemplateSyntaxError at / ‘mptt_tags’ is not a registered tag library. when running Django Application

Posted on

Introduction

This is an article where it is actually the continuation from a previous article. It is an article ‘How to Solve Error Message TemplateSyntaxError at / Invalid block tag on line 11: ‘recursetree’. when displaying Django MPTT Tree Model in Django Application. Apparently, after resolving the problem at that article, there is a chance that another error appear. The error appear as in the following :

TemplateSyntaxError at /

'mptt_tags' is not a registered tag library. Must be one of:
admin_list
admin_modify
admin_urls
cache
i18n
l10n
log
static
tz

Before going on further, there are several important files which become the base for the Django application’s execution. All of those files actually are part of the application folder exist as in the following structure :

(env) C:\project\apps>dir
Volume in drive C is Windows-SSD
Volume Serial Number is CA30-19A4

Directory of C:\project\apps

09/06/2022 08:19 PM <DIR>       .
09/06/2022 07:13 AM <DIR>       ..
08/24/2022 10:02 AM         215 admin.py
08/24/2022 08:43 AM         146 apps.py
08/26/2022 02:19 PM <DIR>       migrations
08/26/2022 02:05 PM       1,548 models.py
09/06/2022 07:01 PM <DIR>       templates
08/24/2022 08:43 AM          63 tests.py
09/06/2022 08:03 AM         118 urls.py
09/08/2022 06:43 AM         871 views.py
08/24/2022 08:43 AM           0 __init__.py
09/08/2022 06:43 AM <DIR>       __pycache__
7 File(s) 2,961 bytes
6 Dir(s) 66,276,225,024 bytes free

(env) C:\project\apps>

Those files are describing the condition where it will trigger error message :

  1. First of all, the ‘models.py’ file exist in the apps folder. It is a model where there is a model class which is using the MPTT model. Below is the content of the ‘models.py’ file :

    class Department(MPTTModel):
        name = models.CharField(max_length=50, unique=True)
        parent = TreeForeignKey('self',on_delete=models.CASCADE, null=True, blank=True, related_name='children')
        class MPTTMeta:
            level_attr = 'department'
            order_insertion_by=['name']
        def __str__(self):
            return self.name
  2. The second file, the ‘views.py’ file which also exist in the apps folder with the following content :

    from django.shortcuts import render
    from .models import Department
    # Create your views here.
    def index(request):
        departments = Department.objects.all()
        return render(request,"index.html",{'departments':Department.objects.all()})
    
  3. Another file which is the third file is the ‘index.html’ file. That is a Django template file with the following content :

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        {% load mptt_tags %}
        <ul class="root">
            {% recursetree departments %}
            <li>
                {{ node.name }}
                {% if not node.is_leaf_node %}
                    <ul class="children">
                        {{ children }}
                    </ul>
                {% endif %}
            </li>
            {% endrecursetree %}
        </ul>
    </body>
    </html>

    So, adding the ‘{% load mptt_tags %}’ to be able to recognize the ‘recursetree’ block tag only trigger another error message. Th

How to Solve Error Message TemplateSyntaxError at / ‘mptt_tags’ is not a registered tag library

Actually, the error message also with the proper solution is very clear and simple. So, the trigger for the error is the Django template file. It is exist as an explicit hint in part of the error message.
‘mptt_tags’ is not a registered tag library. It is obvious since in the error message ‘TemplateSyntaxError at /’. Furthermore, another explicit hint which is pointing for the cause triggering an error message is ‘mptt_tags’ is not a registered tag library. Although ‘{% load mptt_tags %}’ part of code is the solution so that the ‘recursetree block tag will be recognized for further rendering the Django template file ‘index.html’, Django application does not recognize the ‘mptt_tags’ library. In that case, the following is the actual solution for solving it :

  1. First of all, open the ‘settings.py’ file which exist in the project folder. Below is the structure of the folder describing the location of it :

    (env) C:\project\project>dir
    Volume in drive C is Windows-SSD
    Volume Serial Number is CA30-19A4
    
    Directory of C:\project\project
    
    08/20/2022 05:18 PM <DIR>      .
    09/06/2022 07:13 AM <DIR>      ..
    08/20/2022 05:17 PM        403 asgi.py
    09/08/2022 06:44 AM      3,591 settings.py
    09/06/2022 10:31 AM        816 urls.py
    08/20/2022 05:17 PM        403 wsgi.py
    08/20/2022 05:17 PM          0 __init__.py
    09/08/2022 06:44 AM <DIR>      __pycache__
    5 File(s) 5,213 bytes
    3 Dir(s) 66,270,949,376 bytes free
    
    (env) C:\project\project>
  2. The following is the content of ‘views.py’ file which is very important for registering the ‘mptt_tags’ library. Just find the configuration with the part of ‘

    # Application definition
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'apps.apps.AppsConfig',
    ]

    In order to do that, just add ‘mptt’ in the last line of the INSTALLED_APPS. So, the revision will be exist as follows :

    # Application definition
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'apps.apps.AppsConfig',
        'mptt',
    ]
  3. Since there is a change in the ‘settings.py’ file, the Django application need to be restarted. Just stop the Django application service and then run it once more. If there are no more error message appear, the Django application will run properly.

2 thoughts on “How to Solve Error Message TemplateSyntaxError at / ‘mptt_tags’ is not a registered tag library. when running Django Application

Leave a Reply