【发布时间】:2016-06-13 22:44:29
【问题描述】:
下面的代码在字典中搜索单词,并在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')
【问题讨论】:
-
你尝试了什么?该文档有一个使用 [视图中的分页器](docs.djangoproject.com/en/1.9/topics/pagination/… 的示例。该示例是对查询集
contact_list进行分页,您只需对查询集进行分页word。 -
顺便说一句,如果您将模型命名为
Word(大写,单数),将您的查询集命名为words(可能有多个结果)并使用到处都是render而不是render_to_response。 -
感谢您的建议,我是 Python/Django 的新手,还有很多东西要学。
标签: python django pagination