Skip to content

Instantly share code, notes, and snippets.

@allyjweir
Created January 28, 2015 10:14
Show Gist options
  • Save allyjweir/06605e49ae0cb3019920 to your computer and use it in GitHub Desktop.
Save allyjweir/06605e49ae0cb3019920 to your computer and use it in GitHub Desktop.
autocomplete.py
import simplejson as json
from django.http import HttpResponse
from haystack.query import SearchQuerySet
def autocomplete(request):
sqs = SearchQuerySet().autocomplete(content_auto=request.GET.get('query', ''))[:5]
suggestions = [result.title for result in sqs]
# Make sure you return a JSON object, not a bare list.
# Otherwise, you could be vulnerable to an XSS attack.
the_data = json.dumps({
'results': suggestions
})
return HttpResponse(the_data, content_type='application/json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment