I have two forms in two different tabs in the same template. The models of the forms are connected through a foreign key.
When I'm trying to save each form independently having a submit button for each form, but unfortunately the two forms are trying to save data again, even I push only one submit button.
Views.py
def input(request):
my_demographics = DemographicForm(request.POST or None)
my_diagnosis = DiagnosisForm(request.POST or None)
context = RequestContext(request)
if request.method == 'POST':
submitted = request.POST.get('form_id', '')
if submitted == 'demographics':
# Get the Form1 instance
my_demographics = DemographicForm(request.POST)
if my_demographics.is_valid():
my_demographics_object= my_demographics.save()
my_diagnosis=DiagnosisForm({'patient': my_demographics_object.patient_id})
elif submitted == 'diagnosis':
# Get the Form2 instance
my_diagnosis = DiagnosisForm(request.POST)
if my_diagnosis.is_valid():
my_diagnosis.save()
else:
raise ValueError('No form specified !')
return render_to_response('input.html', {'frm':my_demographics, 'frm_d': my_diagnosis}, context)
input.html
<div class="tab-content">
<!-- Tab panes -->
<form class="tab-pane fade in active" id="demographics" method="post" >
<input type="hidden" name="form_id" value="demographics">
{%crispy frm%}
</form>
<form class="tab-pane fade" id="diagnosis" method="post">
<input type="hidden" name="form_id" value="diagnosis">
{%crispy frm_d%}
</form>
</div>