我厌倦了使用formset创建表单,但表单没有保存。表单从我的模型导入,四个表单具有主表单的外键。我的终端看起来像这样:[12 / Jun / 2017 22:26:20]" POST / model_form / HTTP / 1.1" 200 25267
我的观点:
def careerForm(request):
if request.method == 'POST':
form1 = ModelForm(request.POST or None, request.FILES or None)
formset1 = ModelFormSet(request.POST or None, request.FILES or None)
formset2 = ModelFormSet(request.POST or None, request.FILES or None)
formset3 = ModelFormSet(request.POST or None, request.FILES or None)
formset4 = ModelFormSet(request.POST or None, request.FILES or None)
if form1.is_valid() and formset1.is_valid() and formset2.is_valid() and formset3.is_valid() and formset4.is_valid():
message = "Thank you"
career.vacancy = get_object_or_404(Vacancy, pk = request.POST.get('vacancy'))
form1.save()
formset1.save()
formset2.save()
formset3.save()
formset4.save()
return HttpResponseRedirect('successful')
else:
message = "Something went wrong"
context = { 'message': message }
return render(request, 'library/careerForm.html', context)
else:
form1 = ModelForm()
formset1 = ModelFormSet()
formset2 = ModelFormSet()
formset3 = ModelFormSet()
formset4 = ModelFormSet()
context = { 'form1': form1, 'formset1': formset1, 'formset2': formset2, 'formset3': formset3, 'formset4': formset4 }
return render(request, 'library/careerForm.html', context)
form.py
class ModelForm(forms.ModelForm):
class Meta:
model = Model
fields = '__all__'
exclude = ['is_visible']
ModelFormSet = inlineformset_factory(Model, Model_formset, fields = '__all__', exclude = ['is_visible'])
ModelFormSet = inlineformset_factory(Model, Model_formset, fields = '__all__', exclude = ['is_visible'])
ModelFormSet = inlineformset_factory(Model, Model_formset, fields = '__all__', exclude = ['is_visible'])
ModelFormSet = inlineformset_factory(Model, Model_formset, fields = '__all__', exclude = ['is_visible'])
templete
<form method="POST" action="{% url 'peruse:careerForm' %}" enctype="multipart/form-data">
{% csrf_token %}
</form>
在我的浏览器上输出结果
[output result on my browser][1]