0

I'm trying to return the raw value "Alberto Santos", but in my HTML, the function returns a array. <QuerySet [<Funcionarios: Alberto Santos>]>

My function "funcionarios_nome"

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_nome(self):
        funcionarios = Funcionarios.objects.filter(FuncionarioNome='Alberto Santos')
        return funcionarios

My website, returning all the array

MY HTML

 <p>{{ view.funcionarios_nome }}</p>

I Tried to use .values() function, but i'dont know how to use

  • 1
    I'm not good with class based views, but it is called a `ListView`, so it returns a list (python has lists, similar to arrays). So you can either iterate through the list, `{% for x in view.funcionarios_nome %}` or just get the first by `{{ view.funcionarios_nome.first }}`. Note that `filter` will return a queryset even if that query has only 1 item. – raphael Nov 20 '22 at 23:57

1 Answers1

2

if you are passing data from views to template , it's recommend to use a context

useful links :

What is a context in Django?

If you expect a queryset to already return one row, you can use get() without any arguments to return the object for that row:

ex:

 Funcionarios.objects.filter(EmpresaCodigo=1).get()

source : https://docs.djangoproject.com/en/4.1/ref/models/querysets/#get

Ayman Ferki
  • 196
  • 7