R
R
Rus4962021-10-18 12:37:34
Django
Rus496, 2021-10-18 12:37:34

How to call the count method on a context?

How to call the count method on the context to count the passed model objects when testing the pager? In its current form, the code is as follows, but I would like to avoid counting the length of the list:

response = self.client.get(reverse('index')) self.assertEqual(len(response.context['object_list']), 10)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislava, 2021-12-11
@vladis005

When I did the tudushnik, I did this:
class TaskList(LoginRequiredMixin, ListView):
model = Task
context_object_name = 'tasks'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context[' tasks'] = context['tasks'].filter(user=self.request.user)
context['count'] = context['tasks'].filter(complete=False).count()
context['k' ] = context['tasks'].filter(complete=True).count()
search_input = self.request.GET.get('search-area') or ''
if search_input:
context['tasks'] = context[ 'tasks'].filter(title__icontains=search_input)
context['search_input'] = search_input
return context

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question