Now we'll want to make our simple urls.py that we'll be communicating with (you'll need to update the name of the project and the application in the urls.py file):
Our view is quite simple, simple parsing the incoming GET request, and sending back some serialized data.
fromdjango.utilsimportsimplejsonfromdjango.httpimportHttpResponsefromproject.app.modelsimportBookdefuser_lookup(request):# Default return listresults=ifrequest.method=="GET":ifrequest.GET.has_key(u'query'):value=request.GET[u'query']# Ignore queries shorter than length 3iflen(value)>2:model_results=Book.objects.filter(name__icontains=value)results=[x.nameforxinmodel_results]json=simplejson.dumps(results)returnHttpResponse(json,mimetype='application/json')
Notice we use 'name__icontains' to search by the name of the books. This will probably translate into SQL as a LIKE query, which is historically considered to be a worse idea than delivering a nuclear warhead by a mule. It will likely be quite slow on a large database. This is an example, not production quality code.
And thats it for using JQuery to do some simple autocompletion. Django really does make these things quite simple, although it can be a bit confusing when you're getting started.