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。我想将默认的djangopaginator
替换为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
。要使分页工作正常,只需从HaystacksSearchView
类和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
一起使用。要使分页工作正常,只需从HaystacksSearchView
类和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上测试了这个解决方案,我可以确认它是有效的。谢谢你,伊莱。