我使用 django-pure-pagination 完成了这项工作
网址.py
from haystack.views import SearchView, search_view_factory
from haystack.forms import SearchForm
from myproj.myapp.views import CustomSearchView
urlpatterns += patterns('haystack.views',
url(r'^buscar/$', search_view_factory(
view_class=CustomSearchView,
template='myapp/obj_list.html',
form_class=SearchForm
), name='haystack_search'),
)
视图.py
from haystack.views import SearchView
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
from django.conf import settings
class CustomSearchView(SearchView):
def __name__(self):
return "CustomSearchView"
def extra_context(self):
extra = super(CustomSearchView, self).extra_context()
try:
page = self.request.GET.get('page', 1)
except PageNotAnInteger:
page = 1
RESULTS_PER_PAGE = getattr(settings, 'HAYSTACK_SEARCH_RESULTS_PER_PAGE', 20)
p = Paginator(self.results, RESULTS_PER_PAGE, request=self.request)
pag = p.page(page)
extra['page_obj'] = pag
# important, to make haystack results compatible with my other templates
extra['myobj_list'] = [i.object for i in pag.object_list]
return extra