When trying to submit my ModelFormSet I am getting a MultiValueDictKeyError. The error message is not very descriptive so I'm not sure why the error is being thrown.

Here is the view:

def admin_tools(request):ElectionFormSet=modelformset_factory(Election, exclude=('Complete',), formset=BaseElectionFormSet, extra=0)if request.method=='POST':if 'new_election' in request.POST:new_election=NewElectionForm(request.POST)if new_election.is_valid():election=new_election.save(commit=False)election.save()messages.add_message(request, messages.SUCCESS, 'Election created')return redirect(reverse('elections:home'))elif 'edit_elections' in request.POST:formset=ElectionFormSet(request.POST)if formset.is_valid():formset.save()messages.add_message(request, messages.SUCCESS, 'Election settings saved')return redirect(reverse('elections:home'))else:new_election_form=NewElectionForm()formset=ElectionFormSet()return render(request, 'admin/admin_tools.html',{'new_election': new_election_form,'formset': formset,})

Here is the relevant section of the template:

<div class="card-body"><h4>Toggle election settings:</h4><form method="post" action="http://stackoverflow.com.mevn.net/">{{ formset.management_form }}{% for form in formset %}{% csrf_token %}<div class='card'><div class='card-body w-75 mx-auto'><div class='row'><div class='col-6 text-center'><p>Name<br>{{form.Name}}</p></div><div class='col-6 text-center'><p>Videos<br>{{form.FlipGrid}}</p></div></div><div class='row'><div class='col-12 text-center'><p>Description<br>{{form.Description}}</p></div></div><div class='row'><div class='col-6 text-center'><p>Allow registration: {{form.CandidateReg}}</p></div><div class='col-6 text-center'><p>Allow voting: {{form.VotingOpen}}</p></div></div></div></div>{% endfor %}<div class='text-center'><br><button type="submit" class='btn btn-outline-dark' name='edit_elections'>Save</button></div></form></div>

The error is raise MultiValueDictKeyError(repr(key))django.utils.datastructures.MultiValueDictKeyError: "'form-0-id'"

and it was flagged on the line: if formset.is_valid(): of the view.

How do I resolve this error and get the formset to correctly submit and update the data in the model?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.