Install the django-autocomplete-light>=2.0.0pre
package with pip¶
Install the stable release, preferably in a virtualenv:
pip install django-autocomplete-light
Or the development version:
pip install -e git+https://github.com/yourlabs/django-autocomplete-light.git#egg=autocomplete_light
Append 'autocomplete_light'
to settings.INSTALLED_APPS
before django.contrib.admin
¶
Enable templates and static files by adding autocomplete_light
to
settings.INSTALLED_APPS which is
editable in settings.py
. For example:
INSTALLED_APPS = [
# [...] your list of app packages is here, add this:
'autocomplete_light',
]
Django < 1.7 support: call autocomplete_light.autodiscover()
before admin.autodiscover()
¶
In urls.py
, call autocomplete_light.autodiscover()
before
admin.autodiscover()
and before any import of a form with
autocompletes. It might look like this:
import autocomplete_light.shortcuts as al
# import every app/autocomplete_light_registry.py
al.autodiscover()
import admin
admin.autodiscover()
Also, if you have yourapp.views
which imports a form that has autocomplete,
say SomeForm
, this will work:
import autocomplete_light.shortcuts as al
al.autodiscover()
from yourapp.views import SomeCreateView
But this won’t:
from yourapp.views import SomeCreateView
import autocomplete_light.shortcuts as al
al.autodiscover()
That is because auto-discovery of autocomplete classes should happen before definition of forms using autocompletes.
Include autocomplete_light.urls
¶
Install the autocomplete view and staff debug view in urls.py
using the include function.
Example:
# Django 1.4 onwards:
from django.conf.urls import patterns, url, include
# Django < 1.4:
# from django.conf.urls.default import patterns, url, include
urlpatterns = patterns('',
# [...] your url patterns are here
url(r'^autocomplete/', include('autocomplete_light.urls')),
)
Ensure you understand django.contrib.staticfiles
¶
If you’re just trying this out using the Django runserver, that will take care of staticfiles for you - but for production, you’ll need to understand django-staticfiles to get everything working properly. If you don’t, here’s a good article about staticfiles or refer to the official Django howto and Django topic.
Include autocomplete_light/static.html
after loading jquery.js (>=1.7)¶
Load the javascript scripts after loading jquery.js
, for example by doing:
{% load static %}
<script src="{% static 'admin/js/vendor/jquery/jquery.min.js' %}" type="text/javascript"></script>
{% include 'autocomplete_light/static.html' %}
Optionally include it in admin/base_site.html
too¶
For admin support, override
admin/base_site.html
. For example:
{% extends "admin/base.html" %}
{% load static %}
{% block extrahead %}
{{ block.super }}
<script src="{% static 'admin/js/vendor/jquery/jquery.min.js' %}" type="text/javascript"></script>
{% include 'autocomplete_light/static.html' %}
{% endblock %}
Note
There is nothing magic in how the javascript loads. This means that you can use django-compressor or anything.