我在视图中有两个表单。一个htlm表单包括表单和formset,其他html表单只包含单个表单。
当我尝试提交单一形式(提醒)时,我收到此错误:
[u'ManagementForm data is missing or has been tampered with']
查看:
form_class = CreateEventForm
second_form_class = modelformset_factory(EventTime, form = EventTimeForm, extra=3, formset=EventTimeFormset)
third_form_class = Reminder
model = Event
template_name = 'eventAdmin.html'
def get_context_data(self, **kwargs):
context = super(EventAdminPage, self).get_context_data(**kwargs)
context['form'] = self.form_class(self.request.POST or None, prefix="form", instance = self.object)
context['formset'] = self.second_form_class(
self.request.POST or None,
prefix="formset",
queryset=context['event_times'])
context['reminder_form'] = Reminder(self.request.POST or None)
return context
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="form", instance=self.get_object())
formset = self.second_form_class(request.POST, prefix="formset", queryset=event_times)
if form.is_valid() and formset.is_valid():
event = form.save()
event_times = formset.save()
return HttpResponseRedirect('/event-admin-%s' %self.kwargs['event_id'])
else:
return self.render_to_response(
self.get_context_data(form=form, formset=formset))
if 'reminder_form' in request.POST:
form_class = self.second_form_class
form_name = 'Reminder'
form = form_class(request.POST)
if form.is_valid():
form.save(commit=False)
message = form.cleaned_data.get('text')
return HttpResponseRedirect('/event-admin-%s' %self.kwargs['event_id'])
else:
return self.render_to_response(
self.get_context_data(form=form))
引发错误的表单的HTML:
<form method='POST' action=''><input type='hidden' name='csrfmiddlewaretoken' value='3uQyEn4m8bHpM7Jy6a8WKQz5A8Uf6RZF' />
<div id="div_id_text" class="form-group"> <label for="id_text" class="control-label requiredField">
Text<span class="asteriskField">*</span> </label> <div class="controls "> <textarea class="textarea form-control" cols="40" id="id_text" maxlength="500" name="text" rows="10">
</textarea> </div> </div>
<input class="btn btn-primary" type="submit" name="reminder_form" value="Send" />
</form>
答案 0 :(得分:0)
您需要在%// Change - preallocate
X = zeros(size(img));
%// New - define spatial coordinates
[x,y] = ndgrid(0:size(img,1)-1, 0:size(img,2)-1);
for u=1:size(img,1)
for v=1:size(img,2)
%// Change
a = 2*pi*(u-1)*x/size(img,1);
b = 2*pi*(v-1)*y/size(img,2);
temp = img.*(cos(a+b) + sin(a+b));
%// Change
X(u,v) = sum(temp(:));
end
end
之后添加此行{{ form.management_form }}
。 Django使用它来管理formset中的表单。
<form method='POST' action=''>