gpt4 book ai didi

django - MultiValueDictKeyError django 表单集

转载 作者:行者123 更新时间:2023-12-01 14:35:36 28 4
gpt4 key购买 nike

我正在尝试从表单集中发布数据,但出现了 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>

回溯

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'

最佳答案

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

关于django - MultiValueDictKeyError django 表单集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24191855/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com