问题标签 [django-select2]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
715 浏览

python - 如何在 django 管理站点中使用 django-select2 小部件?

在我的 django 应用程序中,我修改了用户实体以包含一个工作字段 (OneToOneField)。但是从 django 管理站点该字段产生了这么多结果,因此登录用户很难选择工作人员。有什么方法可以使用 django 管理站点中的 select2 (ModelSelect2Widget) 小部件?对于任何常规形式,我都通过以下方式定义了小部件:

有没有办法在管理站点表单中将 ModelSelect2Widget 用于工作人员字段?这是我的代码:

0 投票
0 回答
74 浏览

jquery - 为什么 django-select2 ModelSelect2Widget 小部件没有检索结果

我正在使用 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

这是表单的定义

这是完整的控制台日志

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 未定义

0 投票
1 回答
227 浏览

python - Django autocomplete-light ModelSelect2Multiple 选择一个有效的选择错误

我不断收到“选择一个有效的选择。'X' 不是可用的选择之一。” 其中'X'是我在填写团队名称,选择游戏和团队成员后提交创建团队表单时配置文件的主键。

我一直在尝试为与 Profile 模型具有 ManyToMany 关系的模型创建一个表单。在尝试了 django-select2 包后,我决定使用 django-autocomplete-light。

到目前为止,这是我的代码

模型.py

视图.py

网址.py

表格.py

create_team.html模板

0 投票
1 回答
29 浏览

python - Django 表单在没有数据的情况下初始化 ModelMultipleChoiceField 时不验证

我想在我的应用程序中使用ModelMultipleChoiceFieldwith 。select2Django

这是forms.py

一切都很好,除了一件事。Symbol表有大约 5000 条记录,当 html 呈现时,所有数据都传递给 html 模板。我不想要它。

我确实将我的表格更改为:

在没有任何数据的情况下初始化表单。出现了一个新问题:提交表单时,它是无效的,django 说我选择的符号不是有效的选择。

事实上,我的问题是我想创建没有数据的表单,然后能够用我从select2.

我能做些什么?

0 投票
0 回答
202 浏览

jquery - 我无法使用 val() 函数为 django-select2 ModelSelect2Widget 设置值

在我的 django 应用程序中,我有一个 CenterModel 和一个 ProvinceModel,每个中心都有一个 Province_id 字段。我已经为 Province_id 使用 ModelSelect2Widget (django_select2) 为 Center 定义了一个表单。我想使用javascript将所选值设置为模板中的province_id字段,但以下代码不起作用:

但是这些行不起作用,它们没有将值设置为 Province_id 字段。相反,如果该字段具有一些先前选择的值,则“.val()”函数会清除该值。我仔细检查了一下,response.province_id 存储了一个有效的省 ID。为什么我不能为 selct 字段设置值?我正在使用 django-select2==7.7.1

0 投票
0 回答
61 浏览

django - 如何使用 Light Select2Widget 而不是 ModelSelect2Widget 作为 django-select2 的外键

在我的 django 应用程序中,我有一个带有 CenterType 字段(外键)的 CenterModel。我正在使用 django-select2 ModelSelect2Widget 来表示表单中的 CenterType。使用 ModelSelect2Widget 数据不会预渲染到页面上,因此我必须先在搜索框中输入内容,然后才能在 select2 小部件中看到 CenterTypes 名称。由于没有太多的中心类型,有没有办法将所有选项预渲染到页面上,所以我可以看到像这张图片一样的小部件 如您所见,所有选项从一开始就可用

如您所见,所有选项从一开始就可用。我不需要输入任何内容来查看我希望 Center 类型的小部件看起来那样的选项

0 投票
0 回答
61 浏览

python - Django Select2:以与您在拖放中选择的项目相同的顺序保存项目

如果您想通过拖放操作 django select2,那么您需要在代码中添加一些 JS

保存您在表单中选择的数据。

你需要先安装 django-select2 https://pypi.org/project/django-select2/

模型.py

django 表单 我已经创建了三个 django 模型,并且我正在将模型用于 django 表单。

jQuery

0 投票
1 回答
286 浏览

django - Django Select 2 Widget 不工作,已渲染字段但未应用样式

我一直在尝试使用 django-select2 小部件使用多项选择

第 1 步:我安装了 django-select2 使用pip install django-select2

第 2 步:将其添加到已安装的应用程序中

第 3 步:我将以下内容添加到settings.py

第 4 步:我尝试使用小部件

第 5 步:只渲染表单,没有任何类型的循环

结果是

在此处输入图像描述

没有样式应用于选择。

我还尝试在 head 标签中包含样式和脚本(没有帮助)。

我相信小部件正在工作,因为当我从 切换Select2MultipleWidget到 时Select2Widget,它会变为单选

以上是django为多选小部件渲染的html

请帮助获取 django-select2 的功能

0 投票
0 回答
27 浏览

django - Django 管理员使用 django-select2 进行链式选择

我有一个City带有外键的标准模型Country。在我的 django admin 中,我想实现所谓的链式选择,即首先我们选择国家,然后根据它选择城市。如果我也可以先选择城市,然后当场获得国家,那将是完美的。此外,使这个案例变得更复杂的是,我们在一个内联中选择了城市和国家。我该怎么做呢?

0 投票
0 回答
15 浏览

python - 如何在 django 管理面板中使用链式 django_select2 选择?

我从文档中获取了几乎没有变化的指南,有我的 models.py

并创建了 ModelForm,因为文档指南中的简单表单不适合管理面板

在简单的而不是管理员的页面上它工作正常。我只是为了测试而尝试过。我真的只需要在管理面板中。这是来自文档的模板

并查看课程

但是在管理面板中,相同的表单不起作用。Selects 不包含任何项目,也没有显示搜索字段,也没有发送到服务器的请求,我可以看到在简单页面上与 selects 交互时