Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-haystack generic SearchView - no results

I'm tryinig to get haystack working with a class-based generic view according to the documentation here. I can get results from a SearchQuerySet in the shell, so the models are being indexed. But I can't get the view to return a result on the page.

The main reason for using the generic view is that I want to extend later with more SQS logic.

I'm probably missing something obvious...

views.py :

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

from .forms import ProviderSearchForm
from .models import Provider

class ProviderSearchView(SearchView):
    template_name = 'search/provider_search.html'
    form_class    = ProviderSearchForm

    def get_context_data(self, *args, **kwargs):
        """ Extends context to include data for services."""
        context = super(ProviderSearchView, self).get_context_data(*args, **kwargs)
        context['body_attr']        = 'id="provider-search"'
        return context

    def get_queryset(self):
        queryset = super(ProviderSearchView, self).get_queryset()
        return queryset.filter(is_active=True)

search_indexes.py:

from haystack import indexes
from .models import Provider

class ProviderIndex(indexes.SearchIndex, indexes.Indexable):
    text        = indexes.CharField(document=True, use_template=True)
    title       = indexes.CharField(model_attr='name')
    created     = indexes.DateTimeField(model_attr='created')

    def get_model(self):
        return Provider

    def index_queryset(self, using=None):
        "Used when the entire index for model is updated."
        return self.get_model().objects.all()

forms.py

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Submit
from crispy_forms.bootstrap import FieldWithButtons
from haystack.forms import SearchForm
from .models import Provider

class ProviderSearchForm(SearchForm):
    """ Override the form with crispy styles """

    models = [ Provider ]

    def __init__(self, *args, **kwargs):
        super(ProviderSearchForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.disable_csrf = True
        self.helper.form_tag = False
        self.helper.form_show_labels = False
        self.helper.layout = Layout (
            FieldWithButtons(
                Field('q', css_class='form-control input-lg', placeholder="Search providers..."),
                Submit('','Search', css_class='btn btn-lg btn-primary'))
        )

    def get_models(self):
        return self.models

    def search(self):
        sqs = super(ProviderSearchForm, self).search().models(*self.get_models())
        return sqs

    def no_query_found(self):
        return self.searchqueryset.all()
like image 572
deixismou Avatar asked Sep 29 '15 10:09

deixismou


1 Answers

The problem was that my page template was using the wrong variable in the for loop.

The documentation suggests:

for result in page_object.object_list

It should be:

for result in page_obj.object_list

note the template variable is page_obj.

See issue post on GitHub

like image 179
deixismou Avatar answered Nov 08 '22 17:11

deixismou