0

In my html-template I load a list of objects and I'm now trying to combine Django-Pagination (from the docs) and the order_by method.

I tried to put some ordering-links which loads the href="/mylist/?order_by=somefield

It works on the first page. But the order seems to break when I'm clicking the "next page". What's the problem here?

"Next page" link loads the href="mylist/?page={{ results.next_page_number }}

View:

    def Mylist(request):

        order_by = request.GET.get('order_by', 'somedefault')   
        myobjects_list = Mymodel.objects.filter(user=request.user).order_by(order_by)
        paginator = Paginator(myobjects_list, 5)

        page = request.GET.get('page')
        try:
            results = paginator.page(page)
        except PageNotAnInteger:
            results = paginator.page(1)
        except EmptyPage:
            results = paginator.page(paginator.num_pages)

        context = {'results ': results }
        return render_to_response('mylist.html', context, context_instance=RequestContext(request))
user3199840
  • 525
  • 2
  • 13
  • 35

1 Answers1

0

In view:

context = {'results ': results, 'order_by': order_by}

In template:

href="mylist/?page={{ results.next_page_number }}&order_by={{ order_by }}"
  • Not exactly sure where my wrong-thinking was in my first attempt. If you could just explain shortly? Is it that django doesn't store / clears the order_by each time i call the view? – user3199840 Apr 06 '14 at 19:39