I have in my template with a form in which data is collected in 2 Django forms but sent to the view in the same post like this:
<form action="" method="post">
{% csrf_token %}
<div class="form1">
<div>
<label for="field1">field1</label>
{{ form1.field1 }}
</div>
<div>
<label for="field2">field2</label>
{{ form1.field2 }}
</div>
<div>
<label for="field3">field3</label>
{{ form1.field3 }}
</div>
</div>
{{ form2set.management_form }}
<div id='form2set-form-list'>
{% for form in form2set %}
<div class='form2set-form'>
{{ form.as_p }}
</div>
{% endfor %}
</div>
<input type="submit">
</div>
</form>
The data is collected in the view and validated. To validate, I have validations declared in both forms. In case the validations fail because the data is incorrect, they return a raise (depending on which one or ones have failed). This raise is what should be shown on the screen to the user as an error.
If the failed validation is in the second form, it displays the error correctly and without problems. Now, in the case that the failure is in the first form, the page simply reloads and does not show the error. The data already entered in the form is not lost and can be corrected, but the error is not displayed.
My view is something like this:
def create(request):
form1 = Form1(request.POST or None)
form2 = formset_factory(Form2, extra=0)
form2Set = form2(request.POST or None, prefix='participant')
context = {
'form1': form1,
'form2Set': form2Set,
}
if request.method == 'POST':
if form1.is_valid() and form2Set.is_valid():
# Logic
else:
context['form1'] = form1
context['form2Set'] = form2Set
return render(request, 'template', context)