Tuesday, August 18, 2009

Using autocomplete plugin of jQuery with django

1. Download jQuery auto complete plugin here.

2. Unzip it.

3. Copy below files from the unzipped onto your javascript directory.
  • jquery.autocomplete.css
  • jquery.autocomplete.js
  • jquery.bgiframes.min.js
  • jquery.dimensions.js (from jquery.com)
4. Include them.

5. If you want to activate auto complete onto the html tag with id 'qwerty', add following code onto your js code area.

$(document).ready(function(){
  $("#qwerty").autocomplete{
    '/ajax/autocomplete',
    {multiple: true, multipleSeparator: ', '}
  );
});

6. Setup urls.py so that it can handle '/ajax/autocomplete' with ajax_autocomplete module.

7. in views.py, make ajax_autocomplete module like,

def ajax_autocomplete(request):
  if request.GET.has_key('q'):
    # Write your code here (search dictionary or db, etc..)
    # And put it onto res (separated with '\n').
    return HttpResponse(res)
  return HttpResponse()

8. Done. Have fun :)

No comments:

Post a Comment