Search code examples
djangodjango-pagination

Django pagination not showing using class base view


Hi looking for a solution and yet nothing solve to my problem, I do not know why but it is not showing the paginate number in html file. here is structure code:

class ListEmployeeActivity(ListView, LoginRequiredMixin):
    paginate_by = 1
    model = EmployeePersonalDetailsModel
    template_name = 'layout/employee_list_layout.html'
    context_object_name = 'datalist'

    def get_queryset(self):
        return self.model.objects.prefetch_related('ecdm_fk_rn').all()

html file:

{% if is_paginated %}
                <div class="pagination pagination-centered">
                    <ul>
                        {% if datalist.has_previous %}
                        <li><a href="?page={{ datalist.previous_page_number }}"><i class="icon-double-angle-left"></i></a></li>
                        {% endif %}

                        {% for total_pages in datalist.paginator.page_range %}
                        <li><a href="?page={{ total_pages }}">1</a></li>
                        {% endfor %}
                        
                        {% if datalist.has_next %}
                        <li><a href="?page={{ datalist.next_page_number }}"><i class="icon-double-angle-right"></i></a></li>
                        <li><a href="?page={{ datalist.paginator.num_pages }}">Last</a></li>
                        {% endif %}
                    </ul>
                </div>
                {% else %}
                <h3>Pagination not working.</h3>
                {% endif %}

Solution

  • from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    
    class UserListView(ListView):
        model = User
        template_name = 'core/user_list.html'
        context_object_name = 'users' 
        paginate_by = 10
        queryset = User.objects.all()
    

    HTML file.

    <table class="table table-bordered">
      <thead>
        <tr>
          <th>Username</th>
          <th>First name</th>
          <th>Email</th>
        </tr>
      </thead>
      <tbody>
        {% for user in users %}
          <tr>
            <td>{{ user.username }}</td>
            <td>{{ user.first_name }}</td>
            <td>{{ user.email }}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
    
    {% if is_paginated %}
      <ul class="pagination">
        {% if page_obj.has_previous %}
          <li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
        {% else %}
          <li class="disabled"><span>&laquo;</span></li>
        {% endif %}
        {% for i in paginator.page_range %}
          {% if page_obj.number == i %}
            <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
          {% else %}
            <li><a href="?page={{ i }}">{{ i }}</a></li>
          {% endif %}
        {% endfor %}
        {% if page_obj.has_next %}
          <li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
        {% else %}
          <li class="disabled"><span>&raquo;</span></li>
        {% endif %}
      </ul>
    {% endif %}
    

    user this for refernace