试图弄清楚这里出了什么问题。我有一个ModelForm,我需要在三种颜色之间进行无线电选择。我得到以下错误:Django表单错误:“在RadioSelect()上选择一个有效的选项”()
“选择一个有效的选择,这是不是可用选项之一”
models.py:
COLORS = (
('1', 'Röd'),
('2', 'Gul'),
('3', 'Blå'),)
class Poster(models.Model):
title = models.CharField(max_length=100)
colors = models.IntegerField(choices=COLORS, default=2)
forms.py:
class PosterForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PosterForm, self).__init__(*args, **kwargs)
class Meta:
model = Poster
fields = ('title', 'colors')
labels = {
"title": "Rubrik",
"colors": "Färg",
}
widgets = {
'colors': forms.RadioSelect(attrs={'choices': "[(1, 'Röd'), (2, 'Gul'),(3, 'Blå')]"}),
}
template.html:
<div id="id_colors">
<div class="radio"><label for="id_colors_0"><input class="" id="id_colors_0" name="colors" title="" type="radio" value="1" required /> Röd</label></div>
<div class="radio"><label for="id_colors_1"><input checked="checked" class="" id="id_colors_1" name="colors" title="" type="radio" value="2" required /> Gul</label></div>
<div class="radio"><label for="id_colors_2"><input class="" id="id_colors_2" name="colors" title="" type="radio" value="3" required /> Blå</label></div>
</div>
{% if form.colors.errors %}
<div class="alert alert-danger">
<strong>{{ form.colors.errors|escape }}</strong>
</div>
{% endif %}
欢迎任何帮助!
谢谢!当我回家时我会尝试。如果它的工作,我一定会标记它! –
对不起,但没有得到它的工作。 –
有没有其他想法?也许我需要在我的forms.py中定义选项? –