2017-07-24 67 views
0

試圖弄清楚這裏出了什麼問題。我有一個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 %} 

歡迎任何幫助!

回答

0

結果IntegerField並不那麼熱衷於String中的數字值。改變這種方式來使用字母和CharField做了訣竅。

models.py:

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(),* 
    } 

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="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 %} 

感謝鄭此帖這讓我: http://cheng.logdown.com/posts/2015/05/25/django-create-a-radio-input-using-bootstrap3s-inline-style

0

您需要使用元組進行選擇!你很接近,但不是很好。這是它應該看起來如何:

COLORS = [ 
    ('1', 'Röd'), 
    ('2', 'Gul'), 
    ('3', 'Blå') 
] 

看看是否有用。如果有的話,一定要把答案標出來!

+0

謝謝!當我回家時我會嘗試。如果它的工作,我一定會標記它! –

+0

對不起,但沒有得到它的工作。 –

+0

有沒有其他想法?也許我需要在我的forms.py中定義選項? –

相關問題