0
我有一個ModelForm,用戶可以提交將信息保存到數據庫。我想用ModelFormset擴展它,以便用戶可以同時查看和提交多個具有不同信息的相同模型表單。但是,我的POST數據沒有綁定到ModelFormset,所以ModelFormset在is_valid()
上失效爲無效。我看到有一個與request.POST.copy()
相關的數據,它只是重複modelform不驗證在modelformset - django
views.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 %}