Python Django表单错误:";选择一个有效的选项";关于RadioSelect()
试图找出这里出了什么问题。我有一个模型,我需要一个收音机在三种颜色之间选择。我得到以下错误: “选择有效选项。这不是可用选项之一” models.py:Python Django表单错误:";选择一个有效的选项";关于RadioSelect(),python,ajax,django,forms,Python,Ajax,Django,Forms,试图找出这里出了什么问题。我有一个模型,我需要一个收音机在三种颜色之间选择。我得到以下错误: “选择有效选项。这不是可用选项之一” models.py: COLORS = ( ('1', 'Röd'), ('2', 'Gul'), ('3', 'Blå'),) class Poster(models.Model): title = models.CharField(max_length=100) colors = models.IntegerField(
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)
COLORS = (
('r', 'Röd'),
('y', 'Gul'),
('b', 'Blå'),)
class Poster(models.Model):
title = models.CharField(max_length=100)
colors = models.CharField(choices=COLORS, max_length=1)
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å')]"}),
}
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(),*
}
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 %}
<div id="id_colors">
<div class="radio"><label for="id_colors_0"><input class="" id="id_colors_0" name="colors" title="" type="radio" value="r" 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="y" required /> Gul</label></div>
<div class="radio"><label for="id_colors_2"><input class="" id="id_colors_2" name="colors" title="" type="radio" value="b" required /> Blå</label></div>
</div>
{% if form.colors.errors %}
<div class="alert alert-danger">
<strong>{{ form.colors.errors|escape }}</strong>
</div>
{% endif %}
罗德
居尔
基本法
{%if form.colors.errors%}
{{form.colors.errors | escape}}
{%endif%}
很高兴得到任何帮助 您需要使用元组进行选择!你离得很近,但还不是很近。下面是它的外观:
COLORS = [
('1', 'Röd'),
('2', 'Gul'),
('3', 'Blå')
]
看看这是否有效。如果答案正确,一定要把它标对 结果表明IntegerField并不热衷于字符串中的数值。将这种方法改为使用字母和CharField就成功了 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)
COLORS = (
('r', 'Röd'),
('y', 'Gul'),
('b', 'Blå'),)
class Poster(models.Model):
title = models.CharField(max_length=100)
colors = models.CharField(choices=COLORS, max_length=1)
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å')]"}),
}
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(),*
}
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 %}
<div id="id_colors">
<div class="radio"><label for="id_colors_0"><input class="" id="id_colors_0" name="colors" title="" type="radio" value="r" 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="y" required /> Gul</label></div>
<div class="radio"><label for="id_colors_2"><input class="" id="id_colors_2" name="colors" title="" type="radio" value="b" required /> Blå</label></div>
</div>
{% if form.colors.errors %}
<div class="alert alert-danger">
<strong>{{ form.colors.errors|escape }}</strong>
</div>
{% endif %}
罗德
居尔
基本法
{%if form.colors.errors%}
{{form.colors.errors | escape}}
{%endif%}
感谢Cheng的这篇文章,它帮助了我:
谢谢!我回家后会试试的。如果它工作,我一定会标记它!还有其他想法吗?也许我需要在forms.py中定义选项?