1

我想知道为什么我的表单没有呈现。我收到的ValueError错误消息说: Need more than one value to unpack

我理解它为什么这么说(因为表单显示为类对象<points.forms.OffensiveScoringForm object at 0x10323fed0>

forms.py

    from django import forms
    from django.forms import widgets


    SCORING_YARDS = [str(x) for x in range(0,55,5)]
    TD_SCORING= [str(x) for x in range(0,11)]
    INTERCEPTIONS = [str(x) for x in range(-10,1)]
    RECEPTIONS = [str(x) for x in [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.5,2]]
    RECEIVING_YARDS = [str(x) for x in range(0,55,5)]

    class OffensiveScoringForm(forms.Form):
        yards_per_point_passing = forms.ChoiceField(widget=forms.Select, choices=SCORING_YARDS)
        points_per_passing_td = forms.ChoiceField(widget=forms.Select, choices=TD_SCORING)
        points_per_interception = forms.ChoiceField(widget=forms.Select, choices=INTERCEPTIONS)
        points_per_reception = forms.ChoiceField(widget=forms.Select, choices=RECEPTIONS)
        yard_per_point_receiving = forms.ChoiceField(widget=forms.Select, choices=RECEIVING_YARDS)
        points_per_receiving_td = forms.ChoiceField(widget=forms.Select, choices=TD_SCORING)
        yards_per_point = forms.ChoiceField(widget=forms.Select, choices=SCORING_YARDS)
        rushing_td = forms.ChoiceField(widget=forms.Select, choices=TD_SCORING)

任何关于为什么我的表单没有呈现的帮助或见解将不胜感激,感谢您的观看。这是我的代码:

views.py

    from django.shortcuts import render
    from myapp.forms import OffensiveScoringForm

    def home(request):
        form = OffensiveScoringForm()
        return render(request,'base.html',{
            'form':form,
        })

base.html

    <form action = "/nothing/" method="get">
    {{ form }}
    <input type="submit" value="Submit" />
    </form>
4

1 回答 1

1

指定的选择值与预期不符,这不是形式。

选择的值应该是元组列表。在元组中,第一项是实际值,而第二项是显示文本。

您定义值的代码SCORING_YARDS给出:

>>> [str(x) for x in range(0,55,5)]
['0', '5', '10', '15', '20', '25', '30', '35', '40', '45', '50']

相反,您可能想要的是:

>>> [(x, str(x)) for x in range(0,55,5)]
[(0, '0'), (5, '5'), (10, '10'), (15, '15'), (20, '20'), (25, '25'), (30, '30'), (35, '35'), (40, '40'), (45, '45'), (50, '50')]
>>> 

同样,您可能想为其他人修复。

于 2013-07-26T05:40:11.450 回答