TransWikia.com

Django | Request muestra datos pero ListView no muestra ningún dato

Stack Overflow en español Asked on November 10, 2021

Mediante una función request, me muestra toda una lista de datos correctamente:

def station_list(request):
    stations = Station.objects.filter().order_by('name')
    incidencias = Incidencia.objects.all()
    return render(request, 'station/station_list.html', {'stations': stations, 'incidencias': incidencias})

En cambio si lo adapto a una ListView, no me muestra ningún dato:

class StationList(ListView):
    template_name = 'station/station_list.html'
    model = Station
    def station_list():
        stations = Station.objects.filter().order_by('name')
        incidencias = Incidencia.objects.all()
        return render(request,'station/station_list.html', {'stations': stations, 'incidencias': incidencias})

Template:

{% for station in stations %}
        <th scope="row">{{ station.codigo }}</th>
{% endfor %}

¿Qué sucede? Gracias.

One Answer

La clave está en mostrar en el template utilizando object_list en vez de stations:

{% for station in object_list %}
    <th scope="row">{{ station.codigo }}</th>
{% endfor %}

Answered by ret on November 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP