简体   繁体   中英

Django ModelFormSet not submitting correctly

I am having trouble submitting a modelformset in my view. For some reason, the formset is not meeting is_valid criteria, whatever I set the actual data to.

Here is the relevant part of the view:

def admin_tools(request):
    ElectionFormSet = modelformset_factory(Election, exclude=('Complete',), formset=BaseElectionFormSet, extra=0)

    if request.method == 'POST':

        if 'edit_elections' in request.POST:
            election_formset = ElectionFormSet(request.POST)
            # print(formset)
            if election_formset.is_valid():
                election_formset.save()
                messages.add_message(request, messages.SUCCESS, 'Election settings saved')
                return redirect(reverse('elections:home'))
            else:
                messages.add_message(request, messages.ERROR, 'Problem')
                return redirect(reverse('elections:home'))

    else:
        election_formset = ElectionFormSet()

    return render(request, 'admin/admin_tools.html',{
        'formset': election_formset,
    })

Every time I submit the formset I get the problem message indicating the formset is not valid and I don't know why.

Here is form tag in the template:

<form method="post" action="">

    {{ 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><p>{{form.id}}</p>
            </div>
        </div>
    {% endfor %}
    <div class='text-center'>
        <br><button type="submit" class='btn btn-outline-dark' name='edit_elections'>Save</button>
    </div>
</form>

I'm not sure what the error would be here. My thought is that maybe each form should be submitted individually and not as a whole formset? I'm also not sure my csrf token is in the right place.

How do I get my formset to submit correctly?

When the formset is invalid, you typically don't redirect. That allows you to re-display the formset along with errors.

In your case, you are rendering the formset manually, so you are not displaying any errors. You could start with the simplest possible template.

<form method="post" action="">
    <table>
        {{ formset }}
    </table>
</form>

Once you're happy the view works, you can customize the template more. See the docs on using formsets in templates for more info.

If you really want to redirect when the formset is invalid, you can check check formset.errors in your view to debug the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM