1
我正在嘗試遍歷MultipleSelect窗體,該窗體利用CheckboxSelectMultiple小部件並顯示其中的每個複選框。下面是迭代的代碼:Django迭代過CheckboxSelectMultiple呈現數字
{% 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)。我也試過基於widget的選擇渲染,使用下面的代碼,作爲一種替代方案:
{% for choice in form.mp4_rasters.field.widget.choices %}
<label class="checkbox inline">
{{ choice }}
</label>
{% endfor %}
該代碼可以正確地呈現名字,但後來它不包括一個複選框。我可以添加什麼來使其顯示覆選框以及表單中定義的適當名稱?