简体   繁体   中英

jquery autocomplete tagging

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

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.

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM