How to perform a specific function upon a button click without creating another view?

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.

Why don’t you want to create new URLs?

I’m assuming that those views are being called at different times from different events.

(Also, why are you checking the email address from the session rather than getting the User object from the request? It seems horribly insecure to me.)

I wanted to know if we can multiple functions inside a single view and if it is possible, how do we call it. Yes creating new URLs is the main way to do that, but I wish to find out if there exists another way of defining additional functions inside a single view.

Moreover, to avoid logging again and again upon browser close, I have kept email check under sessions.(You may guide the secure way :slight_smile: ) I am new to Django and I am really thankful for your corrections.

Why? What problem do you think you’re trying to solve?

(Side note: A view is a function. That function may call other functions, but the view itself is a function. Strictly speaking, what you’re asking for doesn’t make sense.)

For managing user authentication, see: Using the Django authentication system | Django documentation | Django