Reputation: 1697
How to display the actual keyword and count in custom search form?
My Haystack custom search is working just fine but I'm having difficulties displaying the actual keyword and count.
When I was using the default Haystack urls the {{ query }}
would display the actual keyword.
When using custom search form the {{ query }}
and {{ query.count }}
is empty.
form.py
class BusinessSearchForm(SearchForm):
def no_query_found(self):
return self.searchqueryset.all()
def search(self):
sqs = super(BusinessSearchForm, self).search()
if not self.is_valid():
return self.no_query_found()
return sqs
view.py
def business_search(request):
form = BusinessSearchForm(request.GET)
businesslist = form.search()
paginator = Paginator(businesslist, 5)
page = request.GET.get('page')
try:
businesslist = paginator.page(page)
except PageNotAnInteger:
businesslist = paginator.page(1)
except EmptyPage:
businesslist = paginator.page(paginator.num_pages)
return render_to_response('businesslist.html', {'businesslist': businesslist},
context_instance=RequestContext(request))
template.html
Your query {{ query }} has returned {{ query.count }} result{{ businesslist|pluralize }}
Upvotes: 0
Views: 862
Reputation: 69
it is very easy as you can return context which is a dict and parse it at the frontend
in views.py file
context = {
'query': query,
'result_list': Regs.objects.filter(pk__in=list_id),
}
return context
in template.html parse it as
{{ object_list.result_list|length }} is the number of results for the query and "{{ object_list.query }}" is the actual query entered by the user.
Upvotes: 2
Reputation: 5597
You'll need to pass on query
as context.
So something like this:
context = {
'query': form.cleaned_data['q'],
'businesslist': businesslist,
'paginator': paginator,
}
return render_to_response('businesslist.html', context, context_instance=RequestContext(request))
And you can use paginator.count
to get number of results.
template.html
Your query {{ query }} has returned {{ paginator.count }} result{{ paginator.count|pluralize }}.
Upvotes: 1