I have created a project wherein a Doctor views all appointment requests from patients and would choose to approve or reject.
Below view is for viewing all patients who have requested for appointment.
def patient_requests(request):
if "email" in request.session:
user_id = User.objects.get(email=request.session['email'])
if user_id.role == "Doctor":
doc_id = Doctor.objects.get(user_id=user_id)
pat_all = Appointment.objects.filter(doctor=doc_id, status="Pending")
return render(request, "application/patients.html", {"user_id":user_id, "doc_id": doc_id, "pat_all": pat_all})
else:
return redirect("profile")
else:
return redirect("login")
Now another 2 views for approving and rejecting the appointment has to be created.
def approve(request, pk):
if "email" in request.session:
user_id = User.objects.get(email=request.session['email'])
if user_id.role == "Doctor":
doc_id = Doctor.objects.get(user_id=user_id)
app_id = Appointment.objects.get(id=pk)
app_id.status = "Approved"
app_id.save()
return redirect("patient-requests")
else:
return redirect("profile")
else:
return redirect("login")
def reject(request, pk):
if "email" in request.session:
user_id = User.objects.get(email=request.session['email'])
if user_id.role == "Doctor":
doc_id = Doctor.objects.get(user_id=user_id)
app_id = Appointment.objects.get(id=pk)
app_id.status = "Rejected"
app_id.save()
return redirect("patient-requests")
else:
return redirect("profile")
else:
return redirect("login")
Now my question is “Can we add the above 2 views in the main view named patient_requests and may call using if statement if exists any possibility?” This we won’t have to create 2 news URLs and as 2 views.
path('patient-requests/', views.patient_requests, name='patient-requests'),
path('approve/<int:pk>/', views.approve, name='approve'),
path('reject/<int:pk>/', views.reject, name='reject')
If there had been a submit with name “Submit”, we could’ve used below syntax to avoid additional view.
if “Submit” in request.POST:
Since there’s no form in my scenario, I couldn’t use the submit button.