views:

164

answers:

3

Can any one tell me how to use tagging auto-complete in django templates?

I have done this in django admin interface but i am confused to how to do it in the template.

Thanks in advance

A: 

This is from a template where I implemented autocomplete

$(document).ready(function() {

    $("#searchbox").autocomplete('/search_stuff/', {
        width: 300,
        multiple: false,
        matchContains: true,
        delay: 900,
        extraParams: {
               s: function() { return $("#status").val(); }
        }
});

Where search_stuff return a text list of all the items that fitted the criteria. Does that help?

i don't know all the javascript and css are imported correctly ..but it didn't still work....can any one suggest some thing new..
Aamir hussain
+1  A: 

In my template i have this code:

$(document).ready(function(){
  $("#tags1").autocomplete("/taglookup/", {
        width: 320,
        multiple: true,
        multipleSeparator: " "
         });
   }

and on my url.py i have this on the urlparttern tuple, it can be anything depending on how you want to wire you views and urls!

(r'^taglookup/$', 'twine.twineapp.views.tag_lookup')

and on my views.py i have the tag_lookup view implemented as:

def tag_lookup(request):
    # Default return list
    results = []
    if request.method == "GET":
        if request.GET.has_key(u'q'):
            value = request.GET[u'q']
            # Ignore queries shorter than length 2
            if len(value) > 2:
               TI = Tag.objects.filter(name__startswith=value.lower())
               results = [ x.name for x in TI]
    return HttpResponse('\n'.join(results), mimetype='text/plain')

PS: Am using the Tagging package, that's why i have the Tag object in the above code.

gath
+2  A: 

You could use my django-tagging-autocomplete reusable app and take advantage of provided TagAutocomplete form widget. You can find out more about using the widget in the documentation under "Using the form widget".

Please note that the app requires you use django-tagging for your tags. You also need to put {{ form.media }} (where "form" is the name of your form) inside the <head> section in your template, to allow the widget to include it's JavaScript files.

Ludwik Trammer
but it still did't work ..all the css and javascript is imported ...
Aamir hussain
"all the css and javascript is imported"... so if you look in the source `jquery.autocomplete.js` is included in the header, and there is a snippet of JavaScript code next to the `<input>` code? It should work. Also make sure that the URL used for retrieving list of tags via AJAX is accessible. If that doesn't help, could you provide me with URL of an example?
Ludwik Trammer
this is very important information but doc does not mention it :( thanks @Ludwik Trammer
soField