[英]How to have a multiple select field in django in the form of a drop down box
Any help is greatly appreciated, I am a newbie in django. 非常感谢您的帮助,我是django的新手。
class studentRegister(forms.Form):
courseList = forms.ModelMultipleChoiceField(queryset=Courses.objects.all())
Thank you and appreciate your time, I just want to modify this type of form so I can multiple select two or more options at a time and that returns in a list maybe? 谢谢,感谢您的宝贵时间,我只想修改这种类型的表格,这样我一次可以选择多个两个或多个选项,而列表中可能会返回?
One idea is work with Bootstrap classes and Python. 一种想法是使用Bootstrap类和Python。
forms.py forms.py
class yourForm(forms.Form):
options = forms.MultipleChoiceField(
choices=[(option, option) for option in
Options.objects.all()], widget=forms.CheckboxSelectMultiple(),
label="myLabel", required=True, error_messages={'required': 'myRequiredMessage'})
view.py view.py
def anything(...):
(...)
form = yourForm( )
(...)
return render(request, "myPage.html", {'form': form})
myPage.html myPage.html下
(...)
{% csrf_token %}
{% for field in form %}
<div class="col-md-12 dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">{{ field.label_tag }}
<span class="caret"></span>
</button>
<div class="dropdown-menu">
<div><a href="#">{{ field }}</a></div>
</div>
</div>
{% endfor %}
(...)
I think you can use the SelectMultiple
widget. 我认为您可以使用SelectMultiple
小部件。 Source 资源
class studentRegister(forms.Form):
courseList = forms.ModelMultipleChoiceField(queryset=Courses.objects.all(), widget=forms.SelectMultiple)
If this does not fit your needs, you can try using this snippet . 如果这不符合您的需求,则可以尝试使用此代码段 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.