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 q
parameter is provided. Then query
is set to ''
, the if query
condition fails, so results is not set (not even set to None
; Python doesn't know about the name results
at this point). So it fails when you try to get the value from results
to pass it into the context dict for render_to_response
. Perhaps add:
results = None
before:
if query:
....
This way, results
will 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!)