I want to retrieve all indexes under elasticsearch index folder. I got this error.
UnboundLocalError at /tjobfucksearch/
local variable 'results' referenced before assignment
my views.py
from haystack.query import SearchQuerySet
def fucksearch(request):
query = request.GET.get('q', '')
if query:
results = SearchQuerySet().all()
return render_to_response("tjob/fucksearch.html", {
"results": results,
"query": query
})
my urls.py
url(r'^tjobfucksearch/$', 'tjob.views.fucksearch'),
Plus: haystack 2.0.0, django 1.4
Any advice would be appreciated. Plz help me.
Consider the case where no
qparameter is provided. Thenqueryis set to'', theif querycondition fails, so results is not set (not even set toNone; Python doesn’t know about the nameresultsat this point). So it fails when you try to get the value fromresultsto pass it into the context dict forrender_to_response. Perhaps add:before:
This way,
resultswill always be defined by the time you need to pass it to render. (You still have to handle the none-results case in your template!)