Created
January 28, 2015 10:14
-
-
Save allyjweir/06605e49ae0cb3019920 to your computer and use it in GitHub Desktop.
autocomplete.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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