问题描述
所以,一个完整的Django Model Formset Newb问题。我试图保存我的表单并不断收到此错误: ['ManagementForm数据丢失或被篡改' ]
这是我对我的TemplateView的支持:
class AttendanceTemplate(TemplateView):
template_name ='attendance / index.html'
def get_context_data(self,* * kwargs)
context = super(AttendanceTemplate,self).get_context_data(** kwargs)
指令=指令(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)
在attendanceQuery中注册:
出勤,已创建= 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)
上下文['combined'] =合并
返回上下文
这是我如何保存表单:
pre> $ code def def(self,request):
formset = AttendanceFormSet(request.POST)
如果formset.is_valid()
表单中的东西
formset = thing.save()
返回render_to_response(template / index.html,{'formset':formset},RequestContext(请求))
else:
返回HttpResponse(error.msg)
这是我在我的模板:
< form method =POSTaction =>
{%csrf_token%}
{%用于注册,表单合并%}
< 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 =submitclass =save btn btn-default> Save< / button>
< / form>
我保存我的表单错了吗?也许我的循环是错误的?此外,我更愿意单独打印每个字段,所以使用myform.management_Form可能不适用于我? (例如,myform.management_form.field_name)
如果您单独呈现表单,则必须在您的模板中包含管理表单。事实上,您正在压缩您的表单没有任何区别。
包含管理表单很简单,只需添加 {%formset.management_form%}
到您的模板。
< form method =POSTaction =>
{%csrf_token%}
{{formset.management_form}}
{%用于注册,表单合并%}
...
为了正常工作,您需要确保 formset
在模板上下文,例如:
context ['formset'] = formset
/ pre>
您可能会在很有用。从最简单的选项
{{formset}}
开始测试,然后逐渐自定义模板是一个好主意。这使得当内容出错时更容易调试。目前,您似乎错过了{{form.id}}
。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)
解决方案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 }}
.这篇关于试图保存Django Model Formset,不断得到ManagementForm错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!