django-autocomplete-light 不工作 - select2 不是函数
django-autocomplete-light not working - select2 is not a function
我在 Django 1.8 中使用 django-autocomplete-light。
它有时会失败。这是 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
我重新排列了 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',
)
我在 Django 1.8 中使用 django-autocomplete-light。 它有时会失败。这是 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
我重新排列了 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',
)