Search code examples
pythondjangodjango-modelsdjango-viewsdjango-template-filters

Django filter and count in template tag


I'm trying to filter and count a specific QuerySet in the template with Django template engine. Cant get it to work. The film_list is the context and film is the table and language is a field. Any tips on how to use filter and count at the same time in the template engine? Or should I solve it in another way?

{% if filmlist.film.language == "danish" %}
      {{ film_list.all.count }}
{% endif %}

View

class FilmListView(LoginRequiredMixin, ListView):
    paginate_by = 150
    model = Film
    context_object_name = 'film_list'
    template_name = 'movies/movie_list.html'

Solution

  • You have to iterate over your context to get each object like this

    {% for item in filmlist %}
       {% if item.language == "danish" %}
              {{ film_list.count }}
       {% endif %}
    {% endfor %}