M
M
Mat1lda2020-06-02 13:19:18
Django
Mat1lda, 2020-06-02 13:19:18

Doesn't find Django self-written templatetag, why is it not clear?

Actually, it is in the genres_list.py file
This is how it is located:
movies
templatetags
__init__.py
genres_list.py

This is how it is called in the template
{% load genres_list %}
And this is how it is used:

{% for genre in genres %}
            {{ genre.title }}
{% endfor %}


And here is his code:
from django import template
from ..models import Genre


register = template.Library()


@register.inclusion_tag('movies/movie_list.html')
def genres():
    return {'genres': Genre.objects.all()}


Why doesn't it work? PyCharm swears that it does not see it (here in this line {% load genres_list %} )
And when added to the page, an error appears:

UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: QuerySet.
return self.paginator_class(

Either I'm a blind devil, or somewhere there's a catch, file names, fields checked 10 times and everything is correct

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Alyukov, 2020-06-05
@mvxmvl

  1. Please give the correct name of the function (For example, get_genres())
  2. Specify a more explicit Genre import (from movies.models import Genre)
  3. Return correctly
  4. Include templatetag in .html
  5. In .html call a function and give it a name
  6. Iterate over all the data with the for function

# Пункт 3. Возвращайте так в файле темплейттега
return Genre.objects.all()

# html files
{% get_genres as data_genres %}

# html files
{% for genre in data_genres %}
    {{ genre.title }}
{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question