0

I am creating an Order List page for my e-commerce project and I am trying to add pagination and limit the number of orders showing in one page to 6. I don't know the reason for not working or what might I be missing.

Here is the views.py:

class OrderList(LoginRequiredMixin, ListView):
    model = Order
    template_name = "user_orders.html"
    paginate_by = 6


    def get(self, *args, **kwargs):

        try:
            order = Order.objects.filter(user=self.request.user, ordered=True)
            context = {
                'orders': order
            }
            return render(self.request, 'user_orders.html', context)
        except ObjectDoesNotExist:
            messages.warning(self.request, "You do not have any orders")
            return redirect("/")

Here is the template.html

            <table class="table">
              <thead>
                <tr>
                  <th scope="col">No.</th>
                  <th scope="col">Order Reference</th>
                </tr>
              </thead>
              <tbody>
                <tr>
            {% for order in orders %}
                  <th scope="row">{{ forloop.counter }}</th>
                  <td>{{order.ref_code}}</td>
                </tr>
            {% endfor %}
              </tbody>
            </table>

            <!--Pagination-->
            {% if is_paginated %}
            <nav class="d-flex justify-content-center wow fadeIn">
                <ul class="pagination pg-blue">
                    <!--Arrow left-->
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                            <span class="sr-only">Previous</span>
                        </a>
                    </li>
                    {% endif %}
                    <li class="page-item active">
                        <a class="page-link" href="?page={{ page_obj.number}}">{{ page_obj.number}}
                            <span class="sr-only">(current)</span>
                        </a>
                    </li>
                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                            <span class="sr-only">Next</span>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
            <!--Pagination-->

enter image description here

A_K
  • 731
  • 3
  • 15
  • 40

1 Answers1

0

Without knowing exactly what part isn't working and what your page looks like when rendered, I would suggest trying something like this - add a queryset and change your view to use "context_object_name":

class OrderList(LoginRequiredMixin, ListView):
    model = Order
    template_name = "user_orders.html"
    paginate_by = 6
    context_object_name = 'orders'

    def get(self, *args, **kwargs):
        return render(self.request, 'user_orders.html', context)

     def get_queryset(self):
         return Order.objects.filter(user=self.request.user, ordered=True)

It appears you are passing a context object that is not able to be paginated in your question, but it is hard to tell without more information (such as what part exactly isn't working).

Documentation:

https://docs.djangoproject.com/en/3.0/topics/pagination/

is_paginated not working for django Generic Views

Michael Hawkins
  • 2,793
  • 1
  • 19
  • 33