amr_aly

Saving_2_forms_or_more_in_one_form

Jul 15th, 2021 (edited)
191
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Add gyn data with more than one form
  2. def add_gyno(request, patient_id):
  3.     '''  '''
  4.     if request.method == 'POST':
  5.         obs = ObestetricForm(request.POST, prefix='obs')
  6.         men = MenstrualForm(request.POST, prefix='men')
  7.         if obs.is_valid() and men.is_valid():
  8.             obs_form = obs.save(commit=False)
  9.             obs_form.patient_id = patient_id
  10.             # print(obs_form.patient_id)
  11.             obs_form.save()
  12.             obs_id = obs_form.id
  13.             pat_id = obs_form.patient_id
  14.             obestetric = Obestetric.objects.get(id=obs_id)
  15.  
  16.             men_form = men.save(commit=False)
  17.             men_form.obestetric_id = obs_id
  18.             men_form.patient_id = pat_id
  19.             men_form.save()
  20.            
  21.             # messages.success(request, 'Saving process done ....')
  22.             return redirect(reverse('gyno:edit_gyno', kwargs={
  23.                                                         'obs_id': obs_form.id,
  24.                                                         'patient_id': patient_id,}))
  25.         else:
  26.             messages.success(request, 'Saving process failed ..!!')
  27.             return redirect(reverse('gyno:add_gyno', kwargs={'patient_id': patient_id,}))
  28.     else:
  29.         obs = ObestetricForm(prefix='obs')
  30.         men = MenstrualForm(prefix='men')
  31.  
  32.     context = {
  33.         'patient_id': patient_id,
  34.         'obs_form': obs,
  35.         'men_form': men,
  36.        
  37.     }
  38.     return render(request, 'gyno/add_gyno.html', context)
  39.  
  40.  
  41. ################# in add_gyno.html
  42.  
  43. <form method="post">
  44.     {{ men_form.remain.label_tag }}
  45.     {% render_field men_form.remain ::value="this.remain" id="remain" readonly="readonly" %}
  46.         <br>
  47.     {{ obs_form.hist.label_tag }}
  48.     {% render_field obs_form.hist %}
  49.     <button type="submit" class="btn btn-dark" id="savepatform-btn">
  50.         Save
  51.     </button>
  52. </form>
RAW Paste Data