1

I've got a basic django-haystack SearchForm working OK, but now I'm trying to create a custom search form that includes a couple of extra fields to filter on.

I've followed the Haystack documentation on creating custom forms and views, but when I try to view the form I can only get the error:

ValueError at /search/calibration/

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

Shouldn't basing this on SearchForm take care of returning a HttpResponse object?

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'),
    ....
]
TimJ
  • 399
  • 4
  • 18

2 Answers2

2

Haystack's SearchView is a class based view, you have to call .as_view() class method when adding a urls entry.

url(r'^search/calibration/', views.calibration_search.as_view(), name='calibration_search'),
v1k45
  • 8,070
  • 2
  • 30
  • 38
  • Argh, little things like this still catch me out! Fixing that led me to another error `object has no attribute 'as_view'`, which was because I'd used `def calibration_search(SearchView):` instead of `class calibration_search(SearchView):` At least I can see the form correctly now, the only problem is that it doesn't return any results no matter what I search for... – TimJ Nov 04 '16 at 14:03
  • Have you added the data to search index using `rebuild_index` command? – v1k45 Nov 04 '16 at 14:10
  • I have, I know the index is working as I can view it in the admin interface using haystack-browser and my other (main) search form still works perfectly. For some reason my customised form does not get any results. – TimJ Nov 07 '16 at 16:10
2

This helped me.

"removing the "page" prefix on the search.html template did the trick, and was a good temporary solution. However, it became a problem when it was time to paginate the results. So after looking around, the solution was to use the "page_obj" prefix instead of "page" and everything works as expected. It seems the issue is that the haystack-tutorial assumes the page object is called "page", while certain versions of django its called "page_obj"? I'm sure there is a better answer - I'm just reporting my limited findings."

See this: Django-Haystack returns no results in search form

James L
  • 31
  • 1
  • 3