我正在使用django-haystack
并希望覆盖build_page()
功能。这是build_page()的网址。我想用paginator
替换默认的django django-paginator
。非常感谢: - )
def build_page(self):
"""
Paginates the results appropriately.
In case someone does not want to use Django's built-in pagination, it
should be a simple matter to override this method to do what they would
like.
"""
我写了凌乱的代码。你能帮帮我吗?感谢
class MyView(SearchView):
def build_page(self):
build_page = super(MyView, self).build_page()
page = self.results
return page
答案 0 :(得分:1)
我将django-haystack
与django-pure-pagination
一起使用。要使分页工作,您只需覆盖Haystacks build_page
类和SearchView
render
对象的Page
方法。
urls.py
from core.views import ModifiedSearchView
urlpatterns = patterns('',
url(r'^search/', ModifiedSearchView(), name='haystack_search'),
)
views.py
from haystack.views import SearchView
from pure_pagination.paginator import Paginator
from django.http import Http404
class ModifiedSearchView(SearchView):
def build_page(self):
try:
page_no = int(self.request.GET.get('page', 1))
except (TypeError, ValueError):
raise Http404("Not a valid number for page.")
if page_no < 1:
raise Http404("Pages should be 1 or greater.")
paginator = Paginator(self.results, self.results_per_page, request=self.request)
page = paginator.page(page_no)
return (paginator, page)
search.html
{{ page.render }}
或
{% include "pagination.html" with page_obj=page %}
pagination.html
要修改默认呈现(例如,如果使用引导程序),最简单的方法是将pagination.html
包附带的django-pure-pagination
模板复制到模板目录中并包含{{ 1}}如上所示。
答案 1 :(得分:0)
我使用django-pure-pagination
完成了这项工作urls.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'),
)
views.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