MultiValueDictKeyError django formset

时间:2014-06-12 18:51:58

标签: django formset formsets

我正在尝试从formset发布数据,我得到一个MultiValueDictKeyError

查看

            from django.forms.models import modelformset_factory
            BetFormset = modelformset_factory(Bet, form=Bets, extra=0, max_num=1)
            if request.method == 'POST':
                data = BetFormset(request.POST, prefix='bet')

表格

class Bets(ModelForm):
    error_css_class = 'error'
    exclude = ['',]
    class Meta:
        model = Bet
        widgets = {
            'amt': TextInput(attrs={'class':'bet_amt',}),
            'status': HiddenInput(),
            'parlay':CheckboxInput(attrs={'class':'parlay'}),
            'user':HiddenInput(),
            'offer':HiddenInput(),
            'bet_cat':HiddenInput(),
            }

模型

class Bet(models.Model):
    BET_STATUS = (
        (1, 'Open'),
        (2, 'Submitted'),
        (3, 'Paid'),
     )
    BET_CATS = (
        ('STAGE', 'Stage Winner'),
        ('GC', 'General Classification'),
        ('MTN', 'King of the Mountains'),
        ('SPRNT', 'Sprinter'),
        ('YTH', 'Youth'),
     )
    amt = models.FloatField(null=True, blank=True,verbose_name="bet amount mBtC")
    user = models.ForeignKey(User)
    offer = models.ForeignKey("StageRider")
    status=models.IntegerField(max_length=1,choices=BET_STATUS,default=1)
    parlay=models.BooleanField(null=False, blank=False, default=False)
    bet_cat = models.CharField(null=False, max_length=10,blank = False, choices = BET_CATS)
    class Meta:
        unique_together = ("user","status","offer","bet_cat")
        ordering = ['id']

模板

<form action="" method="post" enctype="multipart/form-data" >

Parlay Amt: <input id="parlay_amt" class="bet_amt" type="text" name="parlay_amt"> <br>

{% csrf_token %}

<table class="table table-striped" id="bet_table"><thead><th>Bet</th><th>Odds</th<th>Amount</th<th>Parlay

</th><th>Remove</th></thead>

<tbody>

{{ bet_formset.management_form }}

{% for tr in bet_formset%}

<tr>ALL MY HIDDEN AND NON_HIDDEN FIELDS </tr>

{% endfor %}

</tbody>

</table>

<input type="submit" id='submit_bet' value="Submit">

</form>

回溯

  

自我

     

QueryDict:u'bet-0-parlay':[u'on'],u'bet-TOTAL_FORMS':[u'1'],   u'bet-INITIAL_FORMS':[u'1'],u'bet-MAX_NUM_FORMS':[u'1'],   u'parlay_amt':[u''],u'bet-0-bet_cat':[u'STAGE'],u'bet-0-status':   [u'1'],u'csrfmiddlewaretoken':[u'B3w0SSpBne8CeY7JppViYQ2fuXf4hhp5'],   u'bet-0-user':[u'1'],u'bet-0-offer':[u'5973'],u'bet-0-amt':   [u'0' ]

     

     

u'bet-0-ID'

1 个答案:

答案 0 :(得分:2)

我不知道这是否是“真正的”解决方案,但如果我将id作为隐藏字段添加到表单中,则不再出现错误