Python Forum

Full Version: Django: View is unable to find attributes of database model
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to make a reservation system for a hotel using Django. I am getting this error when I try to finalize the reservation, I am unable to do so. I believe it is related to the ways that I am trying to get the rooms from the database

Here is my make_reservation view
def make_reservation(request, room_type, check_in_date, check_out_date, guests):
    if request.method == 'POST':
        first_name = request.POST.get('first_name')
        last_name = request.POST.get('last_name')
        email = request.POST.get('email')
        phone_number = request.POST.get('phone_number')

        customer = Customer.objects.create(
            first_name=first_name,
            last_name=last_name,
            email=email,
            phone_number=phone_number
        )

        room_type = request.resolver_match.kwargs['room_type']
        rooms = Room.objects.filter(type=room_type)

        if rooms.exists():
            room = rooms.first()
            
            reservation = Reservation.objects.create(
                    room=room,
                    customer=customer,
                    check_in_date=check_in_date,
                    check_out_date=check_out_date
                )

            room.mark_as_booked()

            return redirect('reservation_success') 
        else:
            print(f"No room of type {room_type} exists.")

    context = {
        'room_type': room_type,
        'check_in_date': check_in_date,
        'check_out_date': check_out_date,
        'guests': guests,
    }

    return render(request, 'make_reservation.html', context)
I believe the issue is here
        room_type = request.resolver_match.kwargs['room_type']
        rooms = Room.objects.filter(type=room_type)

        if rooms.exists():
            room = rooms.first()
            
Here are my models
class Room(models.Model):
    
    TYPES = [
        ('double', 'Double'),
        ('king', 'King'),
        ('two_double', 'Two Double'),
        ('suite', 'Suite')
    ]
    
    room_no = models.IntegerField(unique=True)
    type = models.CharField(max_length=20, choices=TYPES)    
    price = models.IntegerField()
    available = models.BooleanField(default=True) 

    def __str__(self):
        return f"Room: {self.room_no} - ${self.price}"

    def mark_as_booked(self):
        self.available = False
        self.save()

    def mark_as_available(self):
        self.available = True
        self.save()

class Reservation(models.Model):
    room = models.ForeignKey(Room, on_delete=models.CASCADE)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    check_in_date = models.DateField()
    check_out_date = models.DateField()

    def __str__(self):
        return f"Reservation for {self.customer} - Room {self.room.room_no}
Basically it's not showing there are rooms in the db. Any ideas why?