Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重写haystack build_page()函数_Python_Django_Django Haystack_Django Pagination - Fatal编程技术网

Python 重写haystack build_page()函数

Python 重写haystack build_page()函数,python,django,django-haystack,django-pagination,Python,Django,Django Haystack,Django Pagination,我正在使用django haystack并希望覆盖build\u page()函数。这里是的url。我想将默认的djangopaginator替换为django paginator。非常感谢:-) 我写了一些乱七八糟的代码。你能帮帮我吗?谢谢 class MyView(SearchView): def build_page(self): build_page = super(MyView, self).build_page() page = self.results

我正在使用
django haystack
并希望覆盖
build\u page()
函数。这里是的url。我想将默认的django
paginator
替换为
django paginator
。非常感谢:-)

我写了一些乱七八糟的代码。你能帮帮我吗?谢谢

class MyView(SearchView):
  def build_page(self):
    build_page = super(MyView, self).build_page()
    page = self.results
    return page

我使用django纯分页来完成这项工作

url.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'),
)
from core.views import ModifiedSearchView

urlpatterns = patterns('',
    url(r'^search/', ModifiedSearchView(), 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
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)

我使用django纯分页来完成这项工作

url.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'),
)
from core.views import ModifiedSearchView

urlpatterns = patterns('',
    url(r'^search/', ModifiedSearchView(), 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
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)

我使用了带有
django纯分页的
django haystack
。要使分页工作正常,只需从Haystacks
SearchView
类和
render
对象中重写
build\u page
方法

url.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'),
)
from core.views import ModifiedSearchView

urlpatterns = patterns('',
    url(r'^search/', ModifiedSearchView(), 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
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)

pagination.html


要修改默认渲染(例如,如果使用引导),最简单的方法是将
django pure pagination
包附带的
pagination.html
模板复制到模板目录中,并包括
pagination.html
,如上图所示。

我使用
django haystack
django pure pagination
一起使用。要使分页工作正常,只需从Haystacks
SearchView
类和
render
对象中重写
build\u page
方法

url.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'),
)
from core.views import ModifiedSearchView

urlpatterns = patterns('',
    url(r'^search/', ModifiedSearchView(), 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
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)

pagination.html


要修改默认渲染(例如,如果使用引导),最简单的方法是将
django pure pagination
包附带的
pagination.html
模板复制到模板目录中,并包括
pagination.html
,如上所示。

我在Haystack 2.0.0上测试了此解决方案,我可以确认它是有效的。谢谢eli。我在Haystack 2.0.0上测试了这个解决方案,我可以确认它是有效的。谢谢你,伊莱。