I am trying to make a basic hotel reservation system with Django. I want the users to be able to input dates on an index page and see a list of available
for the selected dates. The page should then direct the users to a reservation page for that room type (make_reservation.html, make_resrevation view), where they can finalize the reservation.
I am able to run the search and filter individual rooms, but I am unable to do it by room type. Here is the code for the view
Here is the Room and Reservation models
The line in question is line
available_rooms = Room.objects.filter(
~Q(reservation_set__check_in_date__lte=check_out, reservation_set__check_out_date__gte=check_in)
)
Can I set this up to the results by type of rooms rather than individual Room objects? Or what is the best way to achieve this?
Code:
room_types
I am able to run the search and filter individual rooms, but I am unable to do it by room type. Here is the code for the view
Code:
def search_results(request):
check_in = request.POST.get('check_in_date')
check_out = request.POST.get('check_out_date')
check_in_date = datetime.strptime(check_in, '%Y-%m-%d').date()
check_out_date = datetime.strptime(check_out, '%Y-%m-%d').date()
nights = (check_out_date - check_in_date).days
room_type = request.POST.get('room_type') # Assuming you have a form input for selecting room type
available_rooms = Room.objects.filter(
~Q(reservation_set__check_in_date__lte=check_out, reservation_set__check_out_date__gte=check_in)
)
context = {
'check_in': check_in,
'check_out': check_out,
'available_rooms': available_rooms,
'nights': nights,
}
return render(request, 'myapp/search_results.html', context)
Here is the Room and Reservation models
Code:
class Room(models.Model):
room_number = models.CharField(max_length=10)
room_type = models.CharField(max_length=50)
price = models.DecimalField(max_digits=8, decimal_places=2)
reservations = models.ManyToManyField('Reservation', through='RoomReservation', related_name='room_set')
def __str__(self):
return self.room_number
class Reservation(models.Model):
guest_name = models.CharField(max_length=100)
check_in_date = models.DateField()
check_out_date = models.DateField()
room = models.ForeignKey(Room, on_delete=models.CASCADE, related_name='reservation_set')
credit_card_number = models.CharField(max_length=16)
security_code = models.CharField(max_length=3)
name_on_card = models.CharField(max_length=100)
expiration_date = models.CharField(max_length=100)
total_cost = models.DecimalField(max_digits=10, decimal_places=2) # Add this field
def __str__(self):
return self.guest_name
class RoomReservation(models.Model):
room = models.ForeignKey(Room, on_delete=models.CASCADE)
reservation = models.ForeignKey(Reservation, on_delete=models.CASCADE)
date = models.DateField()
The line in question is line
available_rooms = Room.objects.filter(
~Q(reservation_set__check_in_date__lte=check_out, reservation_set__check_out_date__gte=check_in)
)
Can I set this up to the results by type of rooms rather than individual Room objects? Or what is the best way to achieve this?