0

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 %}
User Unknown
  • 1,079
  • 1
  • 7
  • 17
  • What do you get? `Pagination not working.`? Or something else? – Yuri Nudelman Jan 03 '22 at 07:25
  • @YuriNudelman no display buddy, I have 4 data there but I paginate by 1 so that I could see it. when I put something in `is_paginated` it shows, but those conditional statement there are not working. – User Unknown Jan 03 '22 at 07:27

1 Answers1

2
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

Akash Nagtilak
  • 325
  • 1
  • 5
  • 1
    hey ! this is weird! is the `page_obj` is default thing for `pagination` in class base view? – User Unknown Jan 03 '22 at 07:32
  • You can use it. I also use this is an easy and good way. for reference https://simpleisbetterthancomplex.com/tutorial/2016/08/03/how-to-paginate-with-django.html – Akash Nagtilak Jan 03 '22 at 07:34
  • what do I mean , why is it not working on the object it self? then I do not have any idea that it must be use the `page_obj` variable is that a default thing when you use the pagination in class base view? it works actually, I was just confuse why is it like that. – User Unknown Jan 03 '22 at 07:36
  • ok, I got it !! – Akash Nagtilak Jan 03 '22 at 07:39