title: Hotel Reservation system in Django: How to make a room unavailable to other users for a period of time
body: <p>I am building a hotel reservation system for my a-level coursework and my problem is when a user books a room I want to make sure no other user can reserve that room for the duration of the time period that the room is occupied.</p>
  
  <p>I tried using iteration to loop over the dates and have a boolean value that is set to true when the room is reserved and false when it is not.</p>
  
  <p>Here is my models</p>
  
  <pre><code>class Room(models.Model):
  
     name = models.CharField(max_length = 200)
     img = models.ImageField(upload_to='Pictures')
     desc = models.TextField()
     price = models.IntegerField()
     is_reserved = models.BooleanField(default=False)
     number_of_people = models.PositiveIntegerField()
  
     def __str__(self):
         return self.name
  
     class Meta:
         verbose_name = 'Room'
         verbose_name_plural = 'Rooms'
  
   class Reservation(models.Model):
  
      check_in = models.DateField(default=timezone.now)
      check_out = models.DateField()
      room = models.ForeignKey(Room, on_delete = models.CASCADE)
      guest = models.ForeignKey(User, on_delete= models.CASCADE)
  
      class Meta:
         verbose_name = 'Reservation'
         verbose_name_plural = 'Reservations'
  </code></pre>
  
  <p>This is my function in views where it will loop over the checkin and check out dates inputted by the user and make sure that no other user can reserve that room for those dates</p>
  
  <pre><code>def confirm(request, pk = None):
      if request.method == 'POST':
          if pk:
               room_id = Room.objects.get(pk = pk)
               guest_id = request.user
               check_in = request.session['check_in'] 
               check_out = request.session['check_out']
               reservation = Reservation(
               check_in = check_in, 
               check_out = check_out,
               room_id = room_id.id,
               guest_id = guest_id.id
               )
               reservation.save()
  
               book_in = datetime.strptime(check_in, '%Y-%m-%d').date()
               book_out = datetime.strptime(check_out, '%Y-%m-%d').date()
               reserved = False
  
               delta = timedelta(days = 1)
               while book_in &lt;= book_out:
                    room_id.reserved = True
                    book_in += delta
               else:
                    room_id.reserved = False
  
        return render(request, "system/reserve.html", args)
  </code></pre>
  
  <p>The html page where I want to display that the room is reserved if the check in and check out dates match those in the reservation table for the same room</p>
  
  <pre><code>&lt;body&gt;
      &lt;form method="POST" action="{% url 'system:confirm' room.id %}"&gt;
      {% csrf_token %}
      &lt;h1&gt;{{room.name}}&lt;/h1&gt;
      &lt;img src="{{room.img.url}}"&gt;
      &lt;h3 &gt;£{{room.price}}&lt;/h3&gt;
      &lt;h5 &gt;{{room.desc}}&lt;/h5&gt;
      {% if room_id.reserve == True %}
      &lt;h4&gt;This room has been reserved&lt;/h4&gt;
      {% else %}
      &lt;button name="confirm_reservation" type="submit"&gt; Reserve Room 
      &lt;/button&gt;
      {% endif %}
      &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>I expect a message that a room is reserved (shown in my html block) if either the check in or check out dates is in the reservation table and in between the check in and check out dates. For example: if a room is reserved from the 20th august 2019 to the 25th august 2019 then my check in or check out dates must not fall in between and including the 20th august 2019 and the 25th august 2019 if I want to reserve the room i should get message being displayed to me that i can't. Instead I am able to reserve the room anytime I chose since my boolean value is always false. I want to make the value true for only that period of time and if the check in and check out is not in and in between the check in and check out dates in the reservation table say 8th september 2020 to 10th september 2020 then my boolean value should be false and I am able to reserve that room.</p>