amchugh89
amchugh89

Reputation: 1296

MultiValueDictKeyError django formset

I am trying to post data from a formset and I get a MultiValueDictKeyError

View

            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')

Form

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

Model

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']

Template

<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>

Traceback

self

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']

key

u'bet-0-id'

Upvotes: 3

Views: 2068

Answers (1)

amchugh89
amchugh89

Reputation: 1296

I don't know if this is the 'real' solution, but if I add id as a hidden field to the form, then there is no longer an error

Upvotes: 2

Related Questions