Django formset并形成ValidationError

时间:2015-12-12 20:14:03

标签: python django forms formset

当我尝试同时保存表单和formset时,我遇到验证错误。 ValidationError

  

at / event-admin-2 / [u'ManagementForm数据丢失或已经丢失   篡改了']

发布方法:

def post(self, request, **kwargs):
    self.object = self.get_object()
    event_times = EventTime.objects.filter(event = self.object).exclude(start_time = None)
    if 'update_event_form' in request.POST:
        form_class = self.get_form_class()
        form = self.form_class(request.POST, prefix="foo", instance=self.get_object())
        formset = self.second_form_class(request.POST, prefix="foo2", queryset=event_times)
        if form.is_valid() and formset.is_valid():
            return HttpResponseRedirect('/event-admin-%s' %self.kwargs['event_id']) 
        else:
            return self.render_to_response(
                self.get_context_data(form=form, formset=formset))

模板:

<form method='POST' action=''>{%csrf_token%}
                {% crispy formset event_time_formset_helper %}
                <button id='add-time' type="button" class="btn btn-default">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                </button>
                {{ form|crispy }}
                <input class="btn btn-primary" type="submit" name="update_event_form" value="Update" />
            </form>

HTML输出:

<div> <input id="id_form-TOTAL_FORMS" name="form-TOTAL_FORMS" type="hidden" value="4" /> <input id="id_form-INITIAL_FORMS" name="form-INITIAL_FORMS" type="hidden" value="1" /> <input id="id_form-MIN_NUM_FORMS" name="form-MIN_NUM_FORMS" type="hidden" value="1" /> <input id="id_form-MAX_NUM_FORMS" name="form-MAX_NUM_FORMS" type="hidden" value="1000" /> </div>

1 个答案:

答案 0 :(得分:0)

您已在post方法中实例化formset的地方设置prefix='foo',但可能不会在get方法中的任何位置设置。