Django Haystack 自定义搜索表单

Django Haystack Custom Search Form

我有一个基本的 django-haystack SearchForm 可以正常工作,但现在我正在尝试创建一个自定义搜索表单,其中包含几个要过滤的额外字段。

我已经按照 Haystack 文档创建自定义表单和视图,但是当我尝试查看表单时,我只能得到错误:

ValueError at /search/calibration/

The view assetregister.views.calibration_search didn't return an HttpResponse object. It returned None instead.

不应该基于 SearchForm 来处理返回 HttpResponse 对象吗?

forms.py

from django import forms
from haystack.forms import SearchForm

class CalibrationSearch(SearchForm):
    calibration_due_before = forms.DateField(required=False)
    calibration_due_after = forms.DateField(required=False)

    def search(self):
        #First we need to store SearchQuerySet recieved after / from any other processing that's going on
        sqs = super(CalibrationSearch, self).search()

        if not self.is_valid():
            return self.no_query_found()

        #check to see if any date filters used, if so apply filter
        if self.cleaned_data['calibration_due_before']:
            sqs = sqs.filter(calibration_date_next__lte=self.cleaned_data['calibration_due_before'])

        if self.cleaned_data['calibration_due_after']:
            sqs = sqs.filter(calibration_date_next__gte=self.cleaned_data['calibration_due_after'])

        return sqs

views.py

from .forms import CalibrationSearch
from haystack.generic_views import SearchView
from haystack.query import SearchQuerySet


def calibration_search(SearchView):
    template_name = 'search/search.html'
    form_class = CalibrationSearch
    queryset = SearchQuerySet().filter(requires_calibration=True)

    def get_queryset(self):
        queryset = super(calibration_search, self).get_queryset()
        return queryset

urls.py

from django.conf.urls import include, url
from . import views

urlpatterns = [
    ....
    url(r'^search/calibration/', views.calibration_search, name='calibration_search'),
    ....
]

Haystack 的 SearchView 是基于 class 的视图,添加 urls 条目时必须调用 .as_view() class 方法。

url(r'^search/calibration/', views.calibration_search.as_view(), name='calibration_search'),

这对我有帮助。

”删除 search.html 模板上的“page”前缀就可以了,这是一个很好的临时解决方案。但是,当需要对结果进行分页时,它就成了一个问题。所以在环顾四周之后,解决方案是使用“page_obj”前缀而不是“page”,一切都按预期工作。问题似乎在于 haystack-tutorial 假定页面对象称为“page”,而某些版本的django 它叫“page_obj”?我相信有更好的答案 - 我只是报告我的有限发现。“

看到这个: