Trying to save my Django Model Formset, keep getting ManagementForm error?

不羁岁月 提交于 2019-12-12 04:07:47

问题


So, a total Django Model Formset Newb question. I'm trying to save my form and keep getting this error:

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

Here is what I have for my TemplateView:

  class AttendanceTemplate(TemplateView):

         template_name = 'attendance/index.html'

         def get_context_data(self, **kwargs):
             context = super(AttendanceTemplate, self).get_context_data(**kwargs)
             instruction = Instruction(self.request.user.username)

                 sections_list = self.request.GET.getlist('sections_list')
                 term = self.request.GET.get('term', instruction.term)

                 enrollments = Enrollment.objects.using('wisp').prefetch_related('profile').filter(section_id__in=['111111'], term=term)

                 attendanceQuery = Enrollment.objects.using('wisp').prefetch_related('student').filter(section_id__in=['111111'], term=term)


        for enrollment in attendanceQuery:
           attendance, created = Attendance.objects.update_or_create(
             section_id=enrollment.section_id,
             term=enrollment.term,
             first_name=enrollment.student.first_name,
             last_name=enrollment.student.last_name,
             email_address=enrollment.student.email_address,
        )

    something = Attendance.objects.filter(section_id__in=['111111'], term=term)

    formset = AttendanceFormSet(queryset=something)

    combined = zip(enrollments, formset)

    context['combined'] = combined

    return context

Here is how I'm trying to save the form:

def post(self, request):
    formset = AttendanceFormSet(request.POST)
    if formset.is_valid():
        for thing in formset:
            formset = thing.save()
            return render_to_response("template/index.html",{'formset': formset}, RequestContext(request))
    else:
        return HttpResponse(error.msg)

Here is what I have in my template:

            <form method="POST" action="">
               {% csrf_token %}
                    {% for enrollment, form in combined %}
                         <div class="wrapper-formset">
                             <div>
                               {{ form.first_name.value }}
                               {{ form.last_name.value }}
                                {{ form.email_address.value }}
                              </div>
                               <div class="clear-all"></div>
                              </div>
                           {% endfor %}
            <button type="submit" class="save btn btn-default">Save</button>
            </form>

Am I saving my form wrong? Maybe my loop is wrong? Also, I'd prefer to print each field out individually, so using the "myform.management_Form" may not work for me? (e.g., myform.management_form.field_name)


回答1:


If you render the forms separately, then you must include the management form in your template. The fact that you are zipping your forms makes no difference.

Including the management form is easy, just add {% formset.management_form %} to your template.

<form method="POST" action="">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for enrollment, form in combined %}
    ...

For that to work, you'll need to make sure that formset is in the template context, for example:

    context['formset'] = formset

You might find the docs on using model formsets in the template useful. It would be a good idea to start with the simplest option, {{ formset }}, test it, then gradually customize the template. That makes it easier to debug when stuff goes wrong. At the moment it looks like you have missed out {{ form.id }}.



来源:https://stackoverflow.com/questions/36501570/trying-to-save-my-django-model-formset-keep-getting-managementform-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!