Django - Did you forget to register or download this tag?

I created a special tag that I want to use, but Django does not seem to be able to find it. My templatetags directory is configured as follows:

pygmentize.py

 from pygments import highlight from pygments.lexers import get_lexer_by_name from django import template from pygments.formatters.other import NullFormatter register = template.Library() @register.tag(name='code') def do_code(parser,token): code = token.split_contents()[-1] nodelist = parser.parse(('endcode',)) parser.delete_first_token() return CodeNode(code,nodelist) class CodeNode(template.Node): def __init__(self,lang,code): self.lang = lang self.nodelist = code def render(self,context): code = self.nodelist.render(context) lexer = get_lexer_by_name('python') return highlight(code,lexer,NullFormatter()) 

I am trying to use this tag to render code in gameprofile.html .

gameprofile.html

 (% load pygmentize %} {% block content %} <title>{% block title %} | {{ game.title }}{% endblock %}</title> <div id="gamecodecontainer"> {% code %} {{game.code}} {% endcode %} </div> {% endblock content %} 

When I go to gameprofile.html , I get the error message:

Invalid block tag on line 23: "code", expected "end block". Did you forget to register or download this tag?

+9
source share
9 answers

Have you tried this

 {% load games_tags %} 

upstairs instead of pygmentize?

+10
source

The error in this line is: (% load pygmentize %} , invalid tag. Change it to {% load pygmentize %}

+8
source

I had the same problem, this is how I solved it. After the first section of this very good Django lesson, I did the following:

  • Create a new Django application by executing: python manage.py startapp new_app
  • Edit the settings.py file to add the following to the INSTALLED_APPS list: 'new_app',
  • Add a new module to the new_app package named new_app_tags .
  • In the Django HTML template, add the following to the beginning of the file, but after {% extends 'base_template_name.html' %} : {% load new_app_tags %}
  • In the module file new_app_tags create your own template tag (see below).
  • In the same Django HTML template, starting from step 4 above, use your new custom tag: {% multiply_by_two | "5.0" %} {% multiply_by_two | "5.0" %}
  • Celebrate!

Example from step 5 above:

 from django import template register = template.Library() @register.simple_tag def multiply_by_two(value): return float(value) * 2.0 
+2
source
 {% load static %} <img src="{% static "my_app/example.jpg" %}" alt="My image"> 

in your templates, use the static template tag to create the URL for the specified relative path using the configured STATICFILES_STORAGE .

+2
source
 {% load static %} 

Please add this template tag on top of the HTML or base HTML file.

+1
source

For Django 2.2 +, you need to load the static files into the HTML template before using the static keyword.

 {% load staticfiles %} <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"> 

You must also make sure that you define STATIC_URL in setting.py

Finally make sure static files exist in a specific folder

+1
source

An application containing custom tags must be in INSTALLED_APPS . So, are you sure your directory is in INSTALLED_APPS ?

From the documentation :

An application containing custom tags must be in INSTALLED_APPS for the {% load %} tag to work. This is a security feature: it allows you to host Python code for many template libraries on the same host machine without the ability to access all of them for each Django installation.

0
source

In gameprofile.html change the tag {% endblock content %} to {% endblock %} then it will work, otherwise django will not load the final block and give an error.

0
source

You need to change:

 {% endblock content %} 

in

 {% endblock %} 
-1
source

Source: https://habr.com/ru/post/1241686/


All Articles