0

I am a little confused here...

I have assigned paginate_by within my class but it simply does not paginate. The {% if is_paginated %} returns False. I have read many articles about query_set and others but nothing seems to solve my issue.

Here is my original view:

class Clients(ListView):
    paginate_by = 5
    model = Client
    template_name = 'clients.html'

    def get_clients_view(self, request):
        """
        Primary View
        """

        last_name = request.GET.get('last_name', '')
        email = request.GET.get('email', '')
        if request.method == 'GET':
            object_list = Client.objects.filter(last_name__contains=last_name).filter(email__contains=email)

        register_form   = ClientRegisterForm(request.POST)
        remove_form     = ClientRemoveForm(request.POST)
        search_form     = ClientSearchForm(request.GET) 
        update_form     = ClientUpdateForm(request.POST)         

        client_context= {
                    'object_list'   : object_list,
                    'register_form' : register_form,
                    'remove_form'   : remove_form,
                    'search_form'   : search_form,
                    'update_form'   : update_form
                }
        
        return render(request, template_name='clients.html', context=client_context)

As well as my nav:

<nav class="step-links">
                        {% if is_paginated %}
                            {% if page_obj.has_previous %}
                                <a href="?page=1">&laquo; first</a>
                                <a href="?page={{ page_obj.previous_page_number }}">previous</a>
                            {% endif %}
                            {% if page_obj.number %}
                            <p class="current">
                                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
                            </p>
                            {% endif %}
                            {% if page_obj.has_next %}
                                <a href="?page={{ page_obj.next_page_number }}">next</a>
                                <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
                            {% endif %}
                        {% else %}
                            <p> Is not paginated.</p>    
                        {% endif %} 
                    </nav>

1 Answers1

0

Figured out the problem. When using paginator API requires that page_obj be passed as a context variable WITHOUT the standard object_list.

If object_list is passed along with page_obj within a context dict, then object_list overrides the page_obj handler.

client_context= {
                    #'object_list'   : object_list,
                    'register_form' : register_form,
                    'remove_form'   : remove_form,
                    'search_form'   : search_form,
                    'update_form'   : update_form,
                    'page_obj'      : page_obj

                }

after commenting out object_list and passing page_obj it was then accessible from the html template.