1

I'm using django-haystack and want to override build_page() function. here is a url of build_page(). I want to replace the default django paginator with django-paginator. thanks alot :-)

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.
    """

I've written messy code. Will you please help me? thanks

class MyView(SearchView):
  def build_page(self):
    build_page = super(MyView, self).build_page()
    page = self.results
    return page
Shah
  • 649
  • 1
  • 5
  • 11

2 Answers2

1

I used django-haystack with django-pure-pagination. All you have to do, to get the Pagination working is to override the build_page method from Haystacks SearchView class and render the Page object.

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 }}

or

{% include "pagination.html" with page_obj=page %}

pagination.html

To modify the default rendering (for example if you use bootstrap), the simplest way is to copy the pagination.html template that comes bundled with the django-pure-pagination package into your template directory and include the pagination.html as shown above.

elim
  • 1,404
  • 1
  • 16
  • 17
0

I made this work using 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
jperelli
  • 6,988
  • 5
  • 50
  • 85