def autocomplete(request, app_label=None, model=None):
<EOL>if app_label and model:<EOL><INDENT>try:<EOL><INDENT>model = ContentType.objects.get(app_label=app_label, model=model)<EOL><DEDENT>except:<EOL><INDENT>raise Http404<EOL><DEDENT><DEDENT>else:<EOL><INDENT>model = None<EOL><DEDENT>if not request.GET.has_key("<STR_LIT:q>"):<EOL><INDENT>raise Http404<EOL><DEDENT>else:<EOL><INDENT>q = request.GET["<STR_LIT:q>"]<EOL><DEDENT>counts = request.GET.get("<STR_LIT>", "<STR_LIT:all>")<EOL>limit = request.GET.get("<STR_LIT>", <NUM_LIT:10>)<EOL>if model:<EOL><INDENT>tags = Tag.objects.filter(<EOL>items__content_type = model,<EOL>name__istartswith = q<EOL>).distinct()[:limit]<EOL><DEDENT>else:<EOL><INDENT>tags = Tag.objects.filter(<EOL>name__istartswith = q<EOL>).distinct()[:limit]<EOL><DEDENT>if counts == "<STR_LIT:all>":<EOL><INDENT>l = sorted(list(tags),<EOL>lambda x, y: cmp(y.items.all().count(), x.items.all().count())<EOL>)<EOL>tag_list = "<STR_LIT:\n>".join([ '<STR_LIT>' % (tag.name, tag.items.all().count() ) for tag in l if tag])<EOL><DEDENT>elif counts == "<STR_LIT>":<EOL><INDENT>if model:<EOL><INDENT>l = sorted(list(tags),<EOL>lambda x, y:<EOL>cmp(y.items.filter(content_type=model).count(), x.items.filter(content_type=model).count())<EOL>)<EOL>tag_list = "<STR_LIT:\n>".join(<EOL>["<STR_LIT>" % (tag.name, tag.items.filter(content_type=model).count()) for tag in l if tag]<EOL>)<EOL><DEDENT>else:<EOL><INDENT>raise Exception(<EOL>'<STR_LIT>'<EOL>)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>tag_list = "<STR_LIT:\n>".join([tag.name for tag in tags if tag])<EOL><DEDENT>return HttpResponse(tag_list)<EOL>
returns ``\\n`` delimited strings in the form <tag>||(#)

    GET params are ``q``, ``limit``, ``counts``, ``q`` is what the user
    has typed, ``limit`` defaults to 10, and ``counts`` can be "model", "all"
    or, if absent, will default to all - ie a site-wide count.