[Answered ]-How to pass a variable to class based views (ListView)

1๐Ÿ‘

โœ…

By aggregating at the class-level, the query will run when you start the server, and the count will thus always remain that exact number.

You can define this in a function:

class ListaFuncionariosView(ListView):
    model = Funcionarios
    template_name = '../templates/funcionarios/lista_funcionarios.html'
    paginate_by = 10
    ordering = ['FuncionarioCartao']
    queryset = Funcionarios.objects.filter(EmpresaCodigo=1)

    def funcionarios_number(self):
        return Funcionarios.objects.aggregate(total=Count('FuncionarioCartao'))[
            'total'
        ]

and then access the function in the view in the template:

{{ view.functionarios_number }}

Leave a comment