Django Pagination

76 views
Skip to first unread message

Hélio Miranda

unread,
Oct 3, 2013, 9:34:18 AM10/3/13
to django...@googlegroups.com
Hi guys.

I'm here with a problem that does not quite know how to solve.
I have this query:
result = json.dumps([a.get_json() for a in Player.objects.filter(name=namepost)])

But now I want to return the result with paging, and do not really know how to do ... I've been seeing in the documentation to use the Paginator.

But for example when I do this
        p = Paginator(result, 2)
        print p.count

Gives 1609 ... and the result of the query is 3 records.

Someone can help me?

Tom Christie

unread,
Oct 3, 2013, 9:46:54 AM10/3/13
to django...@googlegroups.com
Hi Hélio,

It looks like you're applying pagination to the rendered JSON string itself.
You want to be paginating the queryset itself.

    queryset = Player.objects.filter(name=namepost)
    paginator = Paginator(queryset, 20)
    return json.dumps([item.to_dict() for item in paginator.object_list])

Hope that helps.

Hélio Miranda

unread,
Oct 3, 2013, 10:01:40 AM10/3/13
to django...@googlegroups.com
I'm doing this:
queryset = Player.objects.filter(name=namepost)
        paginator = Paginator(queryset, 20)
    return json.dumps([item.get_json() for item in paginator.object_list])

And my Document is thus:

But it gives me the following error:
'str' object has no attribute 'status_code'
Message has been deleted

Hélio Miranda

unread,
Oct 3, 2013, 10:07:34 AM10/3/13
to django...@googlegroups.com
Already got it, I did so:
queryset = Player.objects.filter(name=namepost)
        paginator = Paginator(queryset, 2)
    return HttpResponse(json.dumps([item.get_json() for item in paginator.object_list]) , content_type='application/json')

But I return all the records ... I should not return only 2?

Roberto López López

unread,
Oct 3, 2013, 10:13:13 AM10/3/13
to django...@googlegroups.com
Try this (I have not tried the code myself, but I think that it should work):

        queryset = Player.objects.filter(name=namepost)
        paginator = Paginator(queryset, 2)
        page = request.GET.get('page')
        try:
            players = paginator.page(page)
        except PageNotAnInteger:
            players = paginator.page(1)
        except EmptyPage:
            players = paginator.page(paginator.num_pages)
        return HttpResponse(json.dumps([players]) , content_type='application/json')
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e7e37ad2-281c-41f4-a0b9-b333294fe1b8%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


-- 

Roberto López López
System Developer
Parallab, Uni Computing
+47 55584091

Roberto López López

unread,
Oct 3, 2013, 10:14:14 AM10/3/13
to django...@googlegroups.com
Better to say:

        queryset = Player.objects.filter(name=namepost)
        paginator = Paginator(queryset, 2)

        page = request.GET.get('page')
        try:
            players = paginator.page(page)
        except PageNotAnInteger:
            players = paginator.page(1)
        except EmptyPage:
            players = paginator.page(paginator.num_pages)
        return HttpResponse(json.dumps(players) , content_type='application/json')


On 10/03/2013 04:07 PM, Hélio Miranda wrote:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e7e37ad2-281c-41f4-a0b9-b333294fe1b8%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Hélio Miranda

unread,
Oct 3, 2013, 10:27:48 AM10/3/13
to django...@googlegroups.com
doing so:
queryset = Player.objects.filter(name=namepost)
        paginator = Paginator(queryset, 2)

        page = request.GET.get('page')
        try:
            players = paginator.page(page)
        except PageNotAnInteger:
            players = paginator.page(1)
        except EmptyPage:
            players = paginator.page(paginator.num_pages)
        return HttpResponse(json.dumps(players) , content_type='application/json')

Gives the following error:
<Page 1 of 2> is not JSON serializable

Testing like this:
queryset = Player.objects.filter(name=namepost)
        paginator = Paginator(queryset, 2)
  return HttpResponse(json.dumps([item.get_json() for item in paginator.page(1)]) , content_type='application/json')

works, but I have to put the page number of manually

Roberto López López

unread,
Oct 3, 2013, 10:29:21 AM10/3/13
to django...@googlegroups.com
On 10/03/2013 04:13 PM, Roberto López López wrote:
return HttpResponse(json.dumps([players]) , content_type='application/json')
better without the square brackets


    return HttpResponse(json.dumps(players) , content_type='application/json')

Hélio Miranda

unread,
Oct 3, 2013, 10:33:56 AM10/3/13
to django...@googlegroups.com
thus still giving the same error

Hélio Miranda

unread,
Oct 3, 2013, 10:52:34 AM10/3/13
to django...@googlegroups.com
Thanks for the help guys, I managed to solve, so I did:

 queryset = Player.objects.filter(name=namepost)
        paginator = Paginator(queryset, 2)

        try: 
             page = int(request.POST.get('page','1')) 
        except ValueError: 
             page = 1 
        try: 
            results = paginator.page(page) 
        except (EmptyPage, InvalidPage): 
            results = paginator.page(paginator.num_pages) 
        return HttpResponse(json.dumps([item.get_json() for item in results.object_list]) , content_type='application/json')
Reply all
Reply to author
Forward
0 new messages