如何在 Django 中对搜索结果进行分页?

Boo*_*tuz 4 python django pagination

下面的代码在字典中搜索单词,并在search.html上呈现结果,所以我需要在该页面上对结果进行分页,我该怎么做?我在这里阅读了文章https://docs.djangoproject.com/en/1.9/topics/pagination/,但我不知道如何将分页代码嵌入到我的中。

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')
Run Code Online (Sandbox Code Playgroud)

ils*_*005 5

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')
Run Code Online (Sandbox Code Playgroud)