Skip to content

Instantly share code, notes, and snippets.

@mitchdowney
Last active December 24, 2015 20:39
Show Gist options
  • Save mitchdowney/6859229 to your computer and use it in GitHub Desktop.
Save mitchdowney/6859229 to your computer and use it in GitHub Desktop.
See anything wrong with this Bootstrap typeahead?
<head>
<script type="text/javascript">
$('.searchAhead').typeahead({
source: function(query, process) {
return $.get('/lookup/university/', { query: query }, function (data) {
return process(data.options);
});
}
});
</script>
</head>
<body>
<form method="post">{% csrf_token %}
<label>My Label</label>
<input type="text" data-provide="typeahead" class="searchAhead" name="i1" />
<input type="submit" value="Go!" class="button" />
</form>
</body>
from everyvote_mini.models import ParentConstituency
from django.http import HttpResponse
from django.utils.html import escape
# TYPEAHEAD PARENT CONSTITUENCY
def typeahead(request, search_type):
resultSet = {}
if search_type == 'university':
if request.method == "GET":
if request.GET.has_key(u'query'):
query = request.GET[u'query']
obj = ParentConstituency.objects.filter(name__contains=query)
results = [ escape(x.name) for x in obj ]
resultSet["options"] = results
return HttpResponse(json.dumps(resultSet), content_type="application/json")
# TYPEAHEAD
url(r'^lookup/?P<search_type>\w+)/$', 'everyvote_mini.views.typeahead.typeahead', name='typeahead'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment