Django: View is unable to find attributes of database model - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Django: View is unable to find attributes of database model (/thread-41254.html) |
Django: View is unable to find attributes of database model - pythonpaul32 - Dec-07-2023 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? RE: Django: View is unable to find attributes of database model - tahirahmedd - May-20-2024 Ensure the room_type value is correctly passed and matches the choices defined in your Room model. For instance, if you pass "king" but it is stored as "King" in the database, there will be a mismatch. Improve error handling to get more information about the failure update this code: from django.shortcuts import render, redirect from django.http import HttpResponse from .models import Room, Reservation, Customer from django.core.exceptions import ObjectDoesNotExist 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') try: customer = Customer.objects.create( first_name=first_name, last_name=last_name, email=email, phone_number=phone_number ) except Exception as e: return HttpResponse(f"Error creating customer: {e}") try: # Filter rooms by type and availability rooms = Room.objects.filter(type=room_type, available=True) if rooms.exists(): room = rooms.first() # Create a reservation reservation = Reservation.objects.create( room=room, customer=customer, check_in_date=check_in_date, check_out_date=check_out_date ) # Mark room as booked room.mark_as_booked() return redirect('reservation_success') else: return HttpResponse(f"No available room of type {room_type} exists.") except ObjectDoesNotExist: return HttpResponse(f"Room type {room_type} does not exist.") except Exception as e: return HttpResponse(f"Error during reservation: {e}") 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) |