0

我正在使用 django-select2 以模型形式表示部门(ForeignKey)。我按照文档安装了 django-select2,包括放置 {{ form.media.js }} 和 {{ form.media.css }} 标签,但由于某种原因,select2 小部件没有检索任何结果。我检查了浏览器控制台日志并显示以下错误:jQuery.Deferred exception: data is undefined S2</</HidePlaceholder.prototype.removePlaceholder@http://127.0.0.1:8000/static/assets/plugins/select2/ js/select2.full.js:4216:24

这是表单的定义

class WorkerForm(forms.ModelForm):

    class Meta:
        model = WorkerModel
        exclude = ("id",)
        widgets = {
            
            'name':forms.TextInput(attrs={'class': 'form-control'}),
            'surname1' : forms.TextInput(attrs={'class': 'form-control'}),
            'surname2' : forms.TextInput(attrs={'class': 'form-control'}),
            'identification':forms.TextInput(attrs={'class': 'form-control'}),
            'birthday_date' :forms.DateInput(attrs={'class': 'form-control datepicker', 'autocomplete': 'off'}),

            'type' :forms.Select(attrs={'class': 'form-control'}),
            'department' : ModelSelect2Widget(model=DepartmentModel, queryset=DepartmentModel.objects.filter(),
                                            search_fields=['name__icontains'],
                                            attrs={'style': 'width: 100%;','data-placeholder':'Seleccionar'}),
            'is_social_service': forms.CheckboxInput(attrs={'class': 'form-control i-checks'}),
            }

这是完整的控制台日志

jQuery.Deferred 异常:数据未定义 S2</</HidePlaceholder.prototype.removePlaceholder@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:4216:24 调用方法/ <@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:598:32 S2</</HidePlaceholder.prototype.append@http://127.0.0.1: 8000/static/assets/plugins/select2/js/select2.full.js:4199:25 调用方法/<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js :598:32 S2</</Results.prototype.bind/<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:1098:12 S2</< /Observable.prototype.invoke@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:655:20 S2</</Observable.prototype.trigger@http:// /127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:645:12 S2</</Select2.prototype.trigger@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:5827:19 S2</</Select2.prototype._registerEvents/< /<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:5670:14 request/$request<@http://127.0.0.1:8000/static/ assets/plugins/select2/js/select2.full.js:3637:17 mightThrow@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3766:29 resolve/ </process<@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3834:12 setTimeout 处理程序8000/static/assets/plugins/select2/js/select2.full.js:3637:17 mightThrow@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3766 :29 解决/</process<@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3834:12 setTimeout 处理程序8000/static/assets/plugins/select2/js/select2.full.js:3637:17 mightThrow@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3766 :29 解决/</process<@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3834:12 setTimeout 处理程序解决/<@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3872:16 fire@http://127.0.0.1:8000/static/assets/plugins /jquery/jquery-3.6.0.js:3500:31 fireWith@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3630:7 fire@http:// /127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3638:10 fire@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6。 0.js:3500:31 fireWith@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3630:7 done@http://127.0.0.1:8000/ static/assets/plugins/jquery/jquery-3.6.0.js:9796:14 回调/<@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:10057 :17 事件处理程序非空send@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:10076:18 ajax@http://127.0.0.1:8000/static/assets/plugins/jquery /jquery-3.6.0.js:9690:15 transport@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:3582:26 request@http://127.0 .0.1:8000/static/assets/plugins/select2/js/select2.full.js:3624:30 S2</</AjaxAdapter.prototype.query@http://127.0.0.1:8000/static/assets/plugins /select2/js/select2.full.js:3661:7 S2</</MinimumInputLength.prototype.query@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js :3937:15 调用方法/<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:598:32 S2</</Select2.prototype._registerEvents/<@ http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:5669:24 S2</</Observable.prototype.invoke@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:655:20 S2</</Observable.prototype.trigger@http://127.0.0.1:8000/static/assets/plugins/ select2/js/select2.full.js:645:12 S2</</Select2.prototype.trigger@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js: 5827:19 S2</</Select2.prototype._registerDropdownEvents/<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:5629:12 S2</</</</ Observable.prototype.invoke@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:655:20 S2</</Observable.prototype.trigger@http:// 127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:649:12 S2</</Search.prototype.handleSearch@http://127.0.0.1:8000/static/assets/ plugins/select2/js/select2.full.js:4174:12 调用方法/<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:598:32 S2</</Search.prototype.bind/<@http://127.0.0.1:8000/static/assets/plugins/ select2/js/select2.full.js:4120:12 dispatch@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:5430:27 add/elemData.handle@ http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:5234:28 EventListener.handleEvent添加@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:5282:12 on/<@http://127.0.0.1:8000/static/assets/plugins /jquery/jquery-3.6.0.js:5182:16 each@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:385:19 each@http:// /127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:207:17 on@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6。 0.js:5181:14 on@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:5906:10 S2</</Search.prototype.bind@http ://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:4114:18 调用方法/<@http://127.0.0.1:8000/static/assets/plugins/select2/ js/select2.full.js:598:32 S2</</CloseOnSelect.prototype.bind@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:4682: 15 调用方法/<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:598:32 S2</</AttachBody.prototype.bind@http://127.0.0.1:8000/static/assets/plugins/select2/ js/select2.full.js:4338:15 调用方法/<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:598:32 S2</</Select2 .prototype._bindAdapters@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2.full.js:5536:19 Select2@http://127.0.0.1:8000/static/assets/ plugins/select2/js/select2.full.js:5418:10 S2</</$.fn.select2/<@http://127.0.0.1:8000/static/assets/plugins/select2/js/select2。 full.js:6762:26 each@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:385:19 each@http://127.0.0.1:8000/ static/assets/plugins/jquery/jquery-3.6.0.js:207:17 S2</$.fn.select2@http://127.0.0.1:8000/static/assets/plugins/select2/js/ select2.full.js:6759:14 initHeavy@http://127.0.0.1:8000/static/django_select2/django_select2.js:48:14 $.fn.djangoSelect2/<@http://127.0.0.1:8000/static/django_select2/django_select2.js: 56:18 每个@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:385:19 $.fn.djangoSelect2@http://127.0.0.1:8000/ static/django_select2/django_select2.js:53:7 @http://127.0.0.1:8000/static/django_select2/django_select2.js:71:26 mightThrow@http://127.0.0.1:8000/static/assets/plugins /jquery/jquery-3.6.0.js:3766:29 resolve/</process<@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3834:12 setTimeout 处理程序djangoSelect2@http://127.0.0.1:8000/static/django_select2/django_select2.js:53:7 @http://127.0.0.1:8000/static/django_select2/django_select2.js:71:26 mightThrow@http:// /127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3766:29 resolve/</process<@http://127.0.0.1:8000/static/assets/plugins/jquery /jquery-3.6.0.js:3834:12 setTimeout 处理程序djangoSelect2@http://127.0.0.1:8000/static/django_select2/django_select2.js:53:7 @http://127.0.0.1:8000/static/django_select2/django_select2.js:71:26 mightThrow@http:// /127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3766:29 resolve/</process<@http://127.0.0.1:8000/static/assets/plugins/jquery /jquery-3.6.0.js:3834:12 setTimeout 处理程序解决/<@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3872:16 fire@http://127.0.0.1:8000/static/assets/plugins /jquery/jquery-3.6.0.js:3500:31 fireWith@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3630:7 fire@http:// /127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3638:10 fire@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6。 0.js:3500:31 fireWith@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:3630:7 ready@http://127.0.0.1:8000/ static/assets/plugins/jquery/jquery-3.6.0.js:4110:13 completed@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:4120:9 EventListener.handleEvent*@http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:4136:11 @http://127.0.0.1:8000/static/assets/plugins /jquery/jquery-3.6.0.js:36:10 @http://127.0.0.1:8000/static/assets/plugins/jquery/jquery-3.6.0.js:40:4 未定义

4

0 回答 0