I have the following ListView which I want to use also for filtering the results
class Membresias(ListView):
model = Cliente
template_name = 'main/membresias.html'
def get_queryset(self):
nombre = self.request.GET.get('nombre')
if nombre is None:
return Cliente.objects.all()
else:
return Cliente.objects.filter(nombre = nombre).values()
when the 'nombre' variable is not None it show the results, PROBLEM: but when I leave it blank or is None, it does not show any records at all and according to the validation the queryset should retrieve all the records.
Here is part of the html for the table
<div class = "container">
<div >
<table >
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Nombre</th>
<th scope="col">Email</th>
<th scope="col">Teléfono</th>
<th scope="col">Activo</th>
</tr>
</thead>
<tbody>
{% for cliente in object_list %}
<tr>
<th scope="row"><a href="{{ cliente.get_absolute_url }}">{{cliente.id}}</a></th>
<td>{{cliente.nombre}}</td>
<td>{{cliente.email}}</td>
<td>{{cliente.telefono}}</td>
{% if cliente.activo == False %}
<td><input type="checkbox" readonly id="Estatus" disabled value={{cliente.activo}}></td>
{% else %}
<td><input type="checkbox" readonly checked id="staticEmail" disabled value={{cliente.activo}}></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
CodePudding user response:
You can check the truthiness of nombre
and if the nobre
is None
or empty (''
), work with:
class Membresias(ListView):
model = Cliente
template_name = 'main/membresias.html'
def get_queryset(self):
nombre = self.request.GET.get('nombre')
if not nombre:
return Cliente.objects.all()
else:
return Cliente.objects.filter(nombre=nombre)
CodePudding user response:
in return Cliente.objects.filter(nombre = nombre).values()
the .values()
method transforms the query from a model instance into a paython dictionary which may be the problem here,
try droping the .values()
return Cliente.objects.filter(nombre = nombre)