django-autocomplete-light不工作 - select2不是一个函数

时间:2016-05-10 15:58:10

标签: django-autocomplete-light

我正在使用django-autocomplete-light和django 1.8。 它有时会失败。这是javascript控制台错误:

 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({

昨天工作正常,一切都没有改变,但今天它没有用。 我也在使用django西装。 我觉得我的django应用程序之间存在javascript不兼容导致这种随机行为,但我不知道如何追查原因。 有什么想法吗?

以下是我的所有代码:

# 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

1 个答案:

答案 0 :(得分:1)

我重新安排了settings.py中的INSTALLED_APPS,似乎修复了它:

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',
)