我使用的是django-select 2:https://django-select2.readthedocs.io/en/latest/index.html
我试图创建一个多标签选择下拉框类似于这样:
https://raw.githubusercontent.com/asyncee/django-easy-select2/master/screenshots/select2_multiple.png
问题是它没有按预期加载,如下所示:
https://media.geeksforgeeks.org/wp-content/uploads/20191201114034/django-forms-multiplechoicefield-11.png
下面是我的代码。我目前使用的Select2TagWidget
,但我已经尝试了以下其他小部件:
ModelSelect2MultipleWidget
Select2MultipleWidget
forms.py
class BankForm(ModelForm):
required_css_class = "required"
formField1 = forms.ModelMultipleChoiceField(
queryset=Bank.objects.all(),
label="Lead Bank",
widget=Select2TagWidget,
)
class Meta:
model = AdditionalBank
fields = [
"formField1",
]
""" Labels for the fields we didn't override """
labels = {}
urls.py
我已将正确的路径添加到urls.py文件中path("select2/", include("django_select2.urls")),
我已经在settings.py
文件中正确设置了redis队列,我很有信心django-select 2的文档中列出的其余配置也设置正确
我的前端表单呈现此html/css:
<select name="lead_bank" lang="None" data-minimum-input-length="1" data-theme="default" data-allow-clear="false" data-tags="true" data-token-separators="\[\",\", \" \"\]" required="" id="id\_lead\_bank" class="django-select2" multiple="">
<option value="1">Citi</option>
<option value="2" selected="">USBank</option>
<option value="3">Wells Fargo</option>
<option value="4" selected="">NY Bank</option>
</select>
请记住,我是非常新的前端开发。主要是一个后端开发。
1条答案
按热度按时间jvlzgdj91#
在这里,我创建了具有ManytoMany的Select2,如下所示
models.py
form.py
views.py
HTML(基本. html)
HTMl(索引. html)
浏览器输出