2

I have tried something like this in views.py:

class HomePage(TemplateView):
     template_name = "clouderp/index.html"

    def get_context_data(self, **kwargs):
        context = super(HomePage, self).get_context_data(**kwargs) 
        qs = Blog.objects.all()
        context['blog_list'] = qs

        page = self.request.GET.get('page')

        paginator = Paginator(qs, 4)

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

        context['users'] = users

        return context

And in template:

{% if users.has_other_pages %}
<ul class="pagination">
  {% if users.has_previous %}
    <li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>
  {% else %}
    <li class="disabled"><span>&laquo;</span></li>
  {% endif %}
  {% for i in users.paginator.page_range %}
    {% if users.number == i %}
      <li class="active"><span>{{ i }} <span class="sr-only"></span></span></li>
    {% else %}
      <li><a href="?page={{ i }}">{{ i }}</a></li>
    {% endif %}
  {% endfor %}
  {% if users.has_next %}
    <li><a href="?page={{ users.next_page_number }}">&raquo;</a></li>
  {% else %}
    <li class="disabled"><span>&raquo;</span></li>
  {% endif %}
</ul>
{% endif %}

I have created a blog application using django
I want to show all my blogs in my main index.html page and also wanted to do pagination for the blogs in my index page...
I was wondering how to do it...
Because the process I have done the blogs are not paginated according to 4 at a time...

Artem Bernatskyi
  • 4,185
  • 2
  • 26
  • 35
Niladry Kar
  • 1,163
  • 4
  • 20
  • 50
  • 2
    It might be simpler to use [`ListView`](https://docs.djangoproject.com/en/2.1/ref/class-based-views/generic-display/#listview) instead of `TemplateView`, because `ListView` has pagination options already. – Alasdair Sep 26 '18 at 10:13
  • `context['blog_list'] = qs` so in your template `blog_list` is not paginated. `users` is paginated (why is a page of `blog_list` called `users` anyway?). – dirkgroten Sep 26 '18 at 20:49

1 Answers1

2

Please try this code as below: in views.py code

    class BookListView(ListView):
    model=Book
    paginate_by=10
    template_name='book/book_list.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        print (context)
        paginator = context['paginator']
        page_numbers_range = 10  # Display 5 page numbers
        max_index = len(paginator.page_range)

        page = self.request.GET.get('page')
        print (self.request)
        current_page = int(page) if page else 1

        start_index = int((current_page - 1) / page_numbers_range) * page_numbers_range
        end_index = start_index + page_numbers_range
        if end_index >= max_index:
            end_index = max_index

        page_range = paginator.page_range[start_index:end_index]
        context['page_range'] = page_range
        return context

book_list = BookListView.as_view()

and the template:

<div class="container">
            <!-- Pagination -->
            {% if is_paginated %}
            <nav>
                <ul class="pagination justify-content-center" style="margin:20px 0">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}">
                        <span>Prev</span>
                    </a>
                    </li>
                {% else %}
                    <li class="disabled page-item">
                    <a class="page-link" href="#">
                        <span>Prev</span>
                    </a>
                    </li>
                {% endif %}

                {% for page in page_range %}
                    <li {% if page == page_obj.number %} class="active page-item" {% endif %}>
                    <a class="page-link" href="?page={{ page }}">{{ page }}</a>
                    </li>
                {% endfor %}
                {% if page_obj.has_next %}
                    <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}">
                        <span>Next</span>
                    </a>
                    </li>
                {% else %}
                    <li {% if not page_obj.has_next %}class="disabled page-item"{% endif %}>
                    <a class="page-link" href="#">
                        <span>Next</span>
                    </a>
                    </li>
                {% endif %}
                </ul>
            </nav>
            {% endif %}
    </div>
sangrim shin
  • 116
  • 1
  • 7