下面的代码在字典中搜索单词,并在 search.html 上呈现结果,因此我需要在该页面上对结果进行分页,我该怎么做?我在这里阅读了#{3}},但我不知道如何将分页代码嵌入到我的代码中。
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')
答案 0 :(得分:4)
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')
答案 1 :(得分:0)
当我需要对查询数据进行分页并覆盖query_set函数时,我更喜欢使用ListView类。像这样......
class FoodMenuView(generic.ListView):
paginate_by = 10 #use your paginated value here
template_name = 'order/_food_menu.html' # your own template
context_object_name = "list_of_food"
def get_queryset(self):
return Food.objects.filter(price=request.GET['price'])