django-autocomplete / autocomplete /

from django.forms import widgets
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse
from django.utils.encoding import force_unicode
from django.forms.util import flatatt

from autocomplete.views import autocomplete

  <input type="hidden" name="%(name)s" id="id_hidden_%(name)s" value="%(hidden_value)s" />
  <input type="text" id="id_%(name)s" value="%(value)s" %(attrs)s />
  <script type="text/javascript">var %(var_name)s = new autocomplete("%(name)s", "%(url)s", %(force_selection)s);</script>

class AutoCompleteWidget(widgets.Widget):


    class Media:
        css = {'all':
        js = (''
              # decomment to enable animation.

    def __init__(self, ac_name, force_selection=True, reverse_label=True,
                 view=autocomplete, attrs=None):
        super(AutoCompleteWidget, self).__init__(attrs)
        self.ac_name = ac_name
        self.force_selection = bool(force_selection)
        self.reverse_label = reverse_label
        self.view = view
    def render(self, name, value, attrs=None):
        var_name = 'ac_%s' % name.replace('-', '_')
        url = reverse(self.view, args=[self.ac_name])
        force_selection = ('false', 'true')[self.force_selection]
        if not value:
            value = hidden_value = u''
        elif self.reverse_label:
            hidden_value = force_unicode(value)
            value = self.view.reverse_label(self.ac_name, value)
            value = hidden_value = force_unicode(value)
        attrs = flatatt(self.build_attrs(attrs))
        return mark_safe(self.AC_TEMPLATE % locals())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.