如何在自定义搜索表单中显示实际的关键字和计数?
我的Haystack自定义搜索工作正常,但我在显示实际关键字和计数方面遇到了困难。
当我使用默认的Haystack网址时,{{ query }}
会显示实际的关键字。
使用自定义搜索表单时,{{ query }}
和{{ query.count }}
为空。
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 }}
答案 0 :(得分:1)
您需要传递query
作为上下文。
这样的事情:
context = {
'query': form.cleaned_data['q'],
'businesslist': businesslist,
'paginator': paginator,
}
return render_to_response('businesslist.html', context, context_instance=RequestContext(request))
您可以使用paginator.count
获取结果数量。
<强> template.html 强>
Your query {{ query }} has returned {{ paginator.count }} result{{ paginator.count|pluralize }}.
答案 1 :(得分:1)
这非常简单,因为您可以返回上下文(这是字典)并在前端对其进行解析
在views.py文件中
context = {
'query': query,
'result_list': Regs.objects.filter(pk__in=list_id),
}
return context
template.html中的解析为
{{ 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.