gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:06 25 4
gpt4 key购买 nike

下面的代码在字典中搜索单词,并在 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')

最佳答案

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')

关于python - 如何在 Django 中对搜索结果进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35724208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com