DEV Community

Discussion on: Building an Appointment Scheduler App with Django and Fauna

Collapse
 
chukslord1 profile image
Chukslord • Edited

This error is a bug that comes up when there is no data added to the events table, therefore no data to be paginated. To resolve this issue simply cache the all_appointment and today_appointment views with a try-except as seen below to handle cases when there is no data.

def today_appointment(request):
    if "user" in request.session:
        try:
            appointments=client.query(q.paginate(q.match(q.index("events_today_paginate"), request.session["user"]["username"],str(datetime.date.today()))))["data"]
            appointments_count=len(appointments)
            page_number = int(request.GET.get('page', 1))
            appointment = client.query(q.get(q.ref(q.collection("Events"), appointments[page_number-1].id())))["data"]
            if request.GET.get("complete"):
                client.query(q.update(q.ref(q.collection("Events"), appointments[page_number-1].id()),{"data": {"status": "True"}}))["data"]
                return redirect("App:today-appointment")
            if request.GET.get("delete"):
                client.query(q.delete(q.ref(q.collection("Events"), appointments[page_number-1].id())))
                return redirect("App:today-appointment")
            context={"count":appointments_count,"appointment":appointment,"page_num":page_number, "next_page": min(appointments_count, page_number + 1), "prev_page": max(1, page_number - 1)}
            return render(request,"today-appointment.html",context)
        except:
            return render(request,"today-appointment.html")
    else:
        return HttpResponseNotFound("Page not found")

def all_appointment(request):
    if "user" in request.session:
        try:
            appointments=client.query(q.paginate(q.match(q.index("events_index_paginate"), request.session["user"]["username"])))["data"]
            appointments_count=len(appointments)
            page_number = int(request.GET.get('page', 1))
            appointment = client.query(q.get(q.ref(q.collection("Events"), appointments[page_number-1].id())))["data"]
            if request.GET.get("delete"):
                client.query(q.delete(q.ref(q.collection("Events"), appointments[page_number-1].id())))
                return redirect("App:all-appointment")
            context={"count":appointments_count,"appointment":appointment, "next_page": min(appointments_count, page_number + 1), "prev_page": max(1, page_number - 1)}
            return render(request,"all-appointment.html",context)
        except:
            return render(request,"all-appointment.html")
    else:
        return HttpResponseNotFound("Page not found")
Enter fullscreen mode Exit fullscreen mode

Hope this helps, thank you.

Collapse
 
josylad profile image
Joseph Adediji

Okay, Cool.
I will try this out.