我有一个模型表单,用户可以提交它以将信息保存到数据库中。我想用 ModelFormset 对其进行扩展,以便用户可以同时查看和提交具有不同信息的多个相同模型表单。但是,我的 POST 数据未绑定到 ModelFormset,因此 ModelFormset 在is_valid()
. 我看到有相关的数据request.POST.copy()
,它只是
视图.py
def create(request):
if request.method == 'POST':
post_data = request.POST.copy()
print "POST DATA"
print post_data
for i in post_data:
print i
formSet = WorkOrder_Form(post_data)
print "FORMSET"
print formSet
if formSet.is_valid():
formSet.save()
else:
print 'INVALID'
return HttpResponseRedirect('/Shelling/')
else:
formSet = formset_factory(WorkOrder_Form, extra=1)
return render_to_response('create.html',{'WorkOrder_Form':formSet}, context_instance=RequestContext(request))
模板:(create.html)
{% load url from future %}
<a href="{% url 'index' %}"> Return to Index </a></li>
<br>
<br>
<form action="{% url 'create' %}" method="post"> {% csrf_token %}
{% for WorkOrder in WorkOrder_Form %}
{{ WorkOrder.as_ul }}
<br>
{% endfor %}