Appointment booking system. Avoid double bookings

I am currently trying to build an appointment system. This works fine however the function doesn't catch if the same appointment has already been booked in the models.

class BookingView(FormView):
    form_class = AvailabilityForm
    template_name = "availability.html"

    def form_valid(self, form):
        data = form.cleaned_data
        bookingList = Appointment.objects.filter()
        
        for booking in bookingList:
            if booking.start > data["end_time"] or booking.end < data["start_time"]:
                booking=Appointment.objects.create(
                    name=data["name"], 
                    start=data["start_time"],
                    end=data["end_time"]
                    )
                booking.save()
                print(booking.start)
                print(data["start_time"])
                return HttpResponse("can be booked")
            else:
                print(booking.start )
                print(data["start_time"])
                return HttpResponse("Cant be booked")
Back to Top