How to Solve Error Message Invalid block tag on line : ‘static’. Did you forget to register or load this tag? on running Django Application

Posted on

Introduction

Running a Django-based application suddenly triggering an error message. The error message exist as in the title of this article. How to Solve Error Message where that error message is “Invalid block tag on line : ‘static’. Did you forget to register or load this tag? on running Django Application”. The message itself showing error appear upon the end of the Django-based internal running service as follows :

(env) C:\python\programming\project\myproject>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
October 18, 2021 - 21:19:17
Django version 3.2.8, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /favicon.ico
[18/Oct/2021 21:19:51,335] - Broken pipe from ('127.0.0.1', 60608)

Internal Server Error: /apps/
Traceback (most recent call last):
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 470, in parse
compile_func = self.tags[command]
KeyError: 'static'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\python\programming\project\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\python\programming\project\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\python\programming\project\myproject\apps\views.py", line 7, in index
return render(request, 'index.html')
File "C:\python\programming\project\env\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\python\programming\project\env\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\python\programming\project\env\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\python\programming\project\env\lib\site-packages\django\template\loader_tags.py", line 183, in render
template = context.template.engine.select_template(template_name)
File "C:\python\programming\project\env\lib\site-packages\django\template\engine.py", line 174, in select_template
return self.get_template(template_name)
File "C:\python\programming\project\env\lib\site-packages\django\template\engine.py", line 143, in get_template
template, origin = self.find_template(template_name)
File "C:\python\programming\project\env\lib\site-packages\django\template\engine.py", line 125, in find_template
template = loader.get_template(name, skip=skip)
File "C:\python\programming\project\env\lib\site-packages\django\template\loaders\base.py", line 29, in get_template
return Template(
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 155, in __init__
self.nodelist = self.compile_nodelist()
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 193, in compile_nodelist
return parser.parse()
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 472, in parse
self.invalid_block_tag(token, command, parse_until)
File "C:\python\programming\project\env\lib\site-packages\django\template\base.py", line 531, in invalid_block_tag
raise self.error(
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 33: 'static'. Did you forget to register or load this tag?
[18/Oct/2021 21:19:51,473] - Broken pipe from ('127.0.0.1', 60607)

Solution

Precisely, the error message in specific appear as part of the above error message below :

...
File "C:\python\programming\project\myproject\apps\views.py", line 7, in index
return render(request, 'index.html')
...

So, the error start at the file with the name of ‘views.py’. Precisely in the part of “return render(request, ‘index.html’)”. For more information, there is an additional message specifying the error message in the last part of the error message as follows :

django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 33: 'static'. Did you forget to register or load this tag?

So, the solution is to check the ‘index.html’ file, specifically in the line 33 where there is a ‘static’ block syntax as part of the tag. It is considered as invalid and it is giving the information on how to solve it. The solution is to register the block tag or to load the block tag. Check it with the following sequences :

  1. First of all, check whether the static block tag is available and it is already registered ?. Just check the ‘settings.py’ file in the project folder. Apparently, after checking the ‘settings.py’ file, there is already a registered block tag of ‘static’ as follows :

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

    It is the ‘django.contrib.staticfiles’ which is representing the registered block tag of ‘static’. So, it is not on the block tag of ‘static’ registration part.

  2. So, the problem lies in the loading of the static block tag. Check the HTML file whether at the top part of that file the loading process of the static block tag is available. Actually, the error line in the HTML file is in this line :

    <img class="d-block w-100" src="{% static 'apps/img/1.png' %}" alt="..." />

    As tracing back to the top of the HTML file, apparently there is no line for loading the static block tag. So, add it as follows :

    {% load static %}
  3. Run the application once more. If there are no error messages appear, it will run properly.

2 thoughts on “How to Solve Error Message Invalid block tag on line : ‘static’. Did you forget to register or load this tag? on running Django Application

  1. i do all correctly as you show but i still get error “Invalid block tag on line 65: ‘static’. Did you forget to register or load this tag?” why

Leave a Reply