Thursday, September 29, 2011

Re: Question about QuerySet and Pagination

On Thu, Sep 29, 2011 at 8:36 AM, Felix Wagner <hsaldinor@googlemail.com> wrote:
> Hello,
>
> I'm currently trying to paginate my results from a search query.
>
> views.py:
>
> def search(request):
>    query = request.GET.get('q', '')
>
>    if query:
>        qset = (
>            Q(NAME__icontains=query)
>        )
>        results = Thin_Client.objects.filter(qset).distinct()
>
>    else:
>        results = []
>
>    paginator = Paginator(results, 25)
>    try:
>        page = int(request.POST.get('page', '1'))
>    except ValueError:
>        page = 1
>    try:
>        thin_client = paginator.page(page)
>    except (EmptyPage, InvalidPage):
>        thin_client = paginator.page(paginator.num_pages)
>
>    return render_to_response("thin_clients/thin_client_search.html",
> {
>        "results": results,
>        "query": query,
>        })
>

You need to actually pass the paginated page ('thin_client') to your
template and iterate over that. You are still iterating over the
unpaginated 'results', so unsurprisingly you don't see the pagination.
See the view example in the docs:

https://docs.djangoproject.com/en/1.3/topics/pagination/#using-paginator-in-a-view

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment