G
G
gromyko212021-01-14 18:12:21
Django
gromyko21, 2021-01-14 18:12:21

Custom template tag how to create 'list_product' is not a registered tag library.?

In general, my application needs the ability to display the value of a variable (queryset). Googling on the Internet, I realized that I need to create my own tag. I also found documentation on this topic, but unfortunately it did not help me.
Tag function

from django import template
from products.models import Genre

register = template.Library()
@register.simple_tag
def list_product(request):
    return {'gerne':Genre.objects.all()}

What do I put in html
{% load list_product %}
      {% for i in  list_product%}
        {{ i }}
     {% endfor %}


Also added to settings
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',

                'home.template_tags.base_tag.list_product', # путь к моей папке
            ],
        },
    },
]


In INSTALLED_APPS the application accordingly lies.
The file with the function is located in myapp/home/template_tags.py, and the __init__.py file is also nearby
.
Catching a bug
TemplateSyntaxError at /

'list_product' is not a registered tag library. Must be one of:

I assume that something is wrong in INSTALLED_APPS
ps you need to insert a variable into the base template

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-01-14
@bacon

I don't know what kind of docks you read
1. what does context_processors have to do with it?
2. put the file with your tags in the templatetags of the corresponding app https://docs.djangoproject.com/en/3.1/howto/custom...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question