tutlu
tutlu

Reputation: 1

ManagementForm data is missing or has been tampered with in django

I am using the following code in my django app which is working fine but i am getting this error when trying to save a form:

['ManagementForm data is missing or has been tampered with']

Views:

def employedit(request, pk, id):
    employ_academic_forms = EmployAcademicUpdateFormSet(queryset=EmployAcademicInfo.objects.filter(employ_id=pk))
    if request.method == 'POST':
        employ_academic_forms = EmployAcademicUpdateFormSet(request.POST)
        if employ_academic_forms.is_valid():
            employ_academic_forms.save()
            return redirect('employ-list')
    context = {
        'employ_academic_forms':employ_academic_forms,
    }
    return render(request, 'admins/employ/edit_employ.html', context)

form:

EmployAcademicUpdateFormSet = modelformset_factory(
    EmployAcademicInfo,
    exclude = ['employ_id'],
    extra=0,
    labels = {
        'degree': 'Enter Employ Degree',
        'last_passing_institution_name': 'Enter Employ Passing Institution',
        'last_passing_year': 'Enter Employ Passing Year',
        
    },
    widgets = {
        'degree' : forms.Select(attrs={'class':'form-control form-control-lg', 'placeholder':'Enter degree'}),
        'last_passing_institution_name' : forms.TextInput(attrs={'class':'form-control form-control-lg', 'placeholder':'Enter institution name'}),
        'last_passing_year' : forms.DateInput(attrs={'class':'form-control form-control-lg', 'type':'date'}),
        
    },   
    )

Html:

{% extends 'base/base.html' %} 
{% load static %}
{% load crispy_forms_tags %} 
{% block content %}
<div class="card">
    <form class="form-horizontal" action="" method="post">
        {% csrf_token %}
        <div class="card-body">
            <div class="card-body">
                <div class="form-horizontal">
                        {{ employAcademicFormSet.management_form }}
                        {% for form in employ_academic_forms %} 
                        {% for field in form.visible_fields %}
                            <div class="form-group row">
                                <label class="col-md-3 col-form-label" for="text-input"><h6>{{ field.label_tag }}</h6></label>
                                <div class="col-md-9">{{ field }}</div>
                            </div>
                        {% endfor %} 
                        {% endfor %}
                    </div>
            </div>
        </div>
        <div class="card-footer">
            <button class="btn btn-lg btn-primary" type="submit">Submit</button>
        </div>
    </form>
</div>
{% endblock %}

Does anyone know what is causing this? Please help me!!!

Upvotes: 0

Views: 246

Answers (1)

NS0
NS0

Reputation: 6096

I think you might be using the wrong name when calling management_form.

Can you try replacing

{{ employAcademicFormSet.management_form }}

with

{{ employ_academic_forms.management_form }}

and see if that helps.

Upvotes: 1

Related Questions