django-autocomplete-light not working - select2 is not a function
Asked Answered
A

1

5

I'm using django-autocomplete-light with django 1.8. It sometimes fails. Here is the javascript console error:

 TypeError: $(...).select2 is not a function


   ;(function ($) {
        $(document).on('autocompleteLightInitialize', '[data-autocomplete-light-function=select2]', function() {
            var element = $(this);
        // This widget has a clear button
        $(this).find('option[value=""]').remove();

        $(this).select2({

It worked fine yesterday and nothing has changed but today it is not working. I'm also using django suit. I feel that there is a javascript incompatibility between my django apps causing this random behavious but I do not know how to track down the cause. Any ideas?

Here is all of my code:

# models.py
class Sample(models.Model):
    name = models.CharField(max_length=200)
    def __unicode__(self):
        return self.name

class Doctor(models.Model):
    name = models.CharField(max_length=200)
    address = models.CharField(max_length=200, null=True, blank=True, default='')
    def __unicode__(self):
        return self.name

class Patient(models.Model):
    name = models.CharField(max_length=200)
    sample = models.OneToOneField(Sample, null=True, blank=True)
    doctor = models.ForeignKey(Doctor, null=True, blank=True, default=None)
    def __unicode__(self):
        return self.name
----------
# urls.py
urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^pat-sam-autocomplete/$', PatientSampleAutocomplete.as_view(), name='pat-sam-autocomplete',),
    url(r'^pat-doc-autocomplete/$', PatientDoctorAutocomplete.as_view(), name='pat-doc-autocomplete',),
)
----------
# views.py
class PatientSampleAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated():
            return Sample.objects.none()

        qs = Sample.objects.all()
        if self.q:
            qs = qs.filter(name__icontains=self.q)

        return qs

class PatientDoctorAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated():
            return Doctor.objects.none()

        qs = Doctor.objects.all()
        if self.q:
            qs = qs.filter(name__icontains=self.q)

        return qs
--------------
# admin.py
from django import forms
from dal import autocomplete

class PatientForm(forms.ModelForm):
    class Meta:
        model = Patient
        fields = ('__all__')
        widgets = {
            'sample': autocomplete.ModelSelect2(url='pat-sam-autocomplete'),
            'doctor': autocomplete.ModelSelect2(url='pat-doc-autocomplete')
        }

@admin.register(Doctor)
class DoctorAdmin(admin.ModelAdmin):
    list_display = ('name', 'address')

@admin.register(Sample)
class SampleAdmin(admin.ModelAdmin):
    list_display = ('name',)

@admin.register(Patient)
class PatientAdmin(admin.ModelAdmin):
    list_display = ('name', 'sample')
    form = PatientForm
Augmented answered 10/5, 2016 at 15:58 Comment(0)
A
1

I rearranged the INSTALLED_APPS in the settings.py and that seem to have fixed it:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'dal',
    'dal_select2',
    'suit',
    'myapp',
    'django.contrib.admin',
    'django_extensions',
    'simple_history',
    'django_tables2',
    'django.contrib.admindocs',
)
Augmented answered 10/5, 2016 at 16:12 Comment(1)
So what was the difference?Treacy

© 2022 - 2024 — McMap. All rights reserved.