Essex
Essex

Reputation: 6118

Issue with Django Paginator

I'm trying to use Django Paginator according to my CBV process but I don't overcome to display pages with my array.

My class looks like :

class IdentityIndividuResearchingView(LoginRequiredMixin, ListView) :

    template_name = 'Identity_Individu_Recherche.html'
    model = Individu
    context_object_name = 'queryset'


    def get_object(self) :

        queryset = Individu.objects.order_by("-id")
        return queryset

    def get_context_data(self, **kwargs) :

        context_data = super(IdentityIndividuResearchingView, self).get_context_data(**kwargs)

        person_France = Individu.objects.filter(Pays='FR')
        context_data['person_France'] = person_France

        if 'recherche' in self.request.GET:

            query_lastname_ID = self.request.GET.get('q1ID')
            query_firstname_ID = self.request.GET.get('q1bisID')
            query_naissance_ID = self.request.GET.get('q1terID')

            sort_params = {}

            lib.Individu_Recherche.set_if_not_none(sort_params, 'Nom__icontains', query_lastname_ID)
            lib.Individu_Recherche.set_if_not_none(sort_params, 'Prenom__icontains', query_firstname_ID)
            lib.Individu_Recherche.set_if_not_none(sort_params, 'VilleNaissance__icontains', query_naissance_ID)

            query_ID_list = Individu.objects.filter(**sort_params)
            context_data['query_ID_list'] = query_ID_list

            paginator = Paginator(query_ID_list, 3)
            page = self.request.GET.get('page', 1)

            try:
                query_ID_list = paginator.page(page)
            except PageNotAnInteger:
                query_ID_list = paginator.page(1)
            except EmptyPage:
                query_ID_list = paginator.page(paginator.num_pages)

And my template looks like this :

<table class="tableform" style="width:85%">
            <tbody>
                <tr>
                    <th>ID</th>
                    <th>État</th>
                    <th>N° Identification</th>
                    <th>Civilité</th>
                    <th>Nom</th>
                    <th>Prénom</th>
                    <th>Date de Naissance</th>
                    <th>Ville de Naissance</th>
                    <th>Pays de Naissance</th>
                    <th>Institution</th>
                </tr>
                {% for item in query_ID_list %}
                <tr>
                    <td>{{ item.id}}</td>
                    <td>{{ item.Etat}}</td>
                    <td>{{ item.NumeroIdentification}}</td>
                    <td>{{ item.Civilite }}</td>
                    <td>{{ item.Nom }}</td>
                    <td>{{ item.Prenom }}</td>
                    <td>{{ item.DateNaissance }}</td>
                    <td>{{ item.VilleNaissance }}</td>
                    <td>{{ item.PaysNaissance }}</td>
                    <td>{{ item.InformationsInstitution }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

            {% if query_ID_list.has_previous %}
            <a href="?page={{ query_ID_list.previous_page_number }}&q1ID={{ request.GET.q1ID }}">Page précédente</a>
            {% endif %}

            <span class="current">
                Page {{ query_ID_list.number }} sur {{ query_ID_list.paginator.num_pages }}.
            </span>

            {% if query_ID_list.has_next %}
                <a href="?page={{ query_ID_list.next_page_number }}&q1ID={{ request.GET.q1ID }}">Page suivante</a>
            {% endif %}

Can you tell me if I forgot something ? I'm getting this :

enter image description here

Upvotes: 0

Views: 122

Answers (1)

Alasdair
Alasdair

Reputation: 308769

It's unusual to set the queryset in get_context_data.

You should override get_queryset to return the Individu queryset. Then if you set paginate_by, the list view will take care of pagination you.

class IdentityIndividuResearchingView(LoginRequiredMixin, ListView) :
    ...
    paginate_by = 3

    def get_queryset(self):
        if 'recherche' in self.request.GET:
            ...
        return Individu.objects.filter(**sort_params)

In the template, you should then loop through {% for item in individu_list %}, and you can also use page_obj and paginator. You might find this answer useful.

See the MultipleObjectMixin docs to see the pagination options you can set on the IdentityIndividuResearchingView class.

Upvotes: 1

Related Questions