I am using haystack to perform a simple search query against my Person model for all the selected filter attributes. I have decide to try out the Faceted Search Form and created a generic TemplateView SearchView.
class SearchView(TemplateView):
template_name = "index.html"
results = EmptySearchQuerySet()
form_class = FacetedSearchForm
form = None
query = ''
selected_people = None
def get(self, request, **kwargs):
self.request = request
self.query = self.get_query()
if self.form.is_valid():
self.results = self.get_results()
context = self.get_context_data(request, **kwargs)
if request.is_ajax() or request.GET.get('ajax'):
self.template_name = 'search/Taleebo/search_results.html'
return self.render_to_response(context)
def get_query(self):
return self.request.GET.get('q')
def get_results(self):
return self.form.search()
All the routing is working fine but when my get method is called it fails at the line
self.form.is_valid()
with an error that says NoneType' object has no attribute 'is_valid'
I am using the default form_class so shouldn’t that attribute be defined?
EDIT:
I am trying to now use the provided SearchView by haystack and the is_valid is no longer an issue but i am not getting any records back from ‘results’. I changed my urls.py to look like this
url(r'^search/person/', search_view_factory(
view_class=SearchView,
template='index.html',
form_class=ModelSearchForm
), name='haystack_search'),
Using any class would result in the same error, when you don’t have an instance to access:
So you’re calling
is_valid()onNone, instead of on an actual form.