我正在尝试遍历使用 CheckboxSelectMultiple 小部件的 MultipleSelect 表单并显示其中的每个复选框。这是迭代的代码:
{% for choice in form.mp4_rasters %}
<label class="checkbox inline">
{{ choice }}
</label>
{% endfor %}
这是我声明的表格:
from django import forms
MP4_CHOICES = ('240p', '360p', '720p', '1080p')
WEBM_CHOICES = MP4_CHOICES
OGG_CHOICES = MP4_CHOICES
MISC_CHOICES = ('MP3', 'Roku')
class BatchSubmitForm(forms.Form):
video_file = forms.FileField()
framerate = forms.FloatField()
title = forms.CharField()
destination = forms.CharField()
mp4_rasters = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=MP4_CHOICES)
webm_rasters = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=WEBM_CHOICES)
ogg_rasters = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=OGG_CHOICES)
misc_rasters = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=MISC_CHOICES)
渲染时,我希望它以我传入的名称输出每个复选框,但不是“240p”、“360p”等。我得到每个复选框的数字(4、6、2、0)。作为替代,我还尝试使用以下代码基于小部件的选择进行渲染:
{% for choice in form.mp4_rasters.field.widget.choices %}
<label class="checkbox inline">
{{ choice }}
</label>
{% endfor %}
该代码适当地呈现名称,但它不包含复选框。我可以添加什么以使其呈现复选框以及表单中定义的适当名称?