【问题标题】:Django form error: "Select a valid choice" on RadioSelect()Django 表单错误:RadioSelect() 上的“选择一个有效的选择”
【发布时间】:2017-07-24 17:38:35
【问题描述】:

试图找出这里出了什么问题。我有一个 ModelForm,我需要在三种颜色之间进行单选。我收到以下错误:

“选择一个有效的选项。这不是可用的选项之一”

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å')]"}),
    }

模板.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 %}

很高兴得到任何帮助!

【问题讨论】:

    标签: python ajax django forms


    【解决方案1】:

    您需要使用元组进行选择!你很接近,但还没有完全打开。它应该是这样的:

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

    看看这是否有效。如果是,请务必将答案标记为正确!

    【讨论】:

    • 谢谢!我回家后试试。如果它有效,我一定会标记它!
    • 还有其他想法吗?也许我需要在 forms.py 中定义选项?
    【解决方案2】:

    事实证明,IntegerField 并不热衷于字符串中的数字值。将这种方法更改为使用字母和 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(),*
        }
    

    模板.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 %}
    

    感谢 Cheng 的这篇文章对我有帮助: http://cheng.logdown.com/posts/2015/05/25/django-create-a-radio-input-using-bootstrap3s-inline-style

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-13
      • 2018-04-30
      • 1970-01-01
      • 2012-05-13
      • 2020-04-14
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      相关资源
      最近更新 更多