Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paginate search results in Django?

The code below searches for the word in dictionary, and render results on search.html, so I need to paginate results on that page, how can I do that? I read the arcticle here https://docs.djangoproject.com/en/1.9/topics/pagination/, but I have no idea how to embed the pagination code to mine.

def search(request):
    if 'results' in request.GET and request.GET['results']:
        results = request.GET['results']
        word = words.objects.filter(title__icontains = results).order_by('title')
        return render_to_response('myapp/search.html',
        {'word': word, 'query': results })
    else:
        return render(request, 'myapp/search.html')
like image 890
Bootuz Avatar asked Jan 07 '23 13:01

Bootuz


1 Answers

from django.core.paginator import Paginator

def search(request):
    if 'results' in request.GET and request.GET['results']:
        page = request.GET.get('page', 1)

        results = request.GET['results']
        word = words.objects.filter(title__icontains = results).order_by('title')
        paginator = Paginator(word, 25) # Show 25 contacts per page
        word = paginator.page(page)
        return render_to_response('myapp/search.html',
                 {'word': word, 'query': results })
    else:
        return render(request, 'myapp/search.html')
like image 180
ilse2005 Avatar answered Jan 12 '23 00:01

ilse2005