Django Tutorial On How To Create A Booking System For A Health Clinic

John Abdsho Khosrowabadi
Dev Genius
Published in
5 min readNov 2, 2022

--

In this tutorial, we are going to create a user-friendly booking system for a health clinic using Django and Python.

Requirements:

  • Have Django and Python installed on your computer
  • Have basic knowledge of the Django framework
  • Familiarity with Django Authentication

Features of this project:

  • Sign-in/Sign-out/Sign-up for users and clinic staff
  • CRUD functionality for users
  • Two-step booking process
  • User Panel
  • Staff Panel

Note: This entire project is uploaded in a public GitHub repository.

Step One: Creating a Django project and Django application

  • Create a Django project called “clinic”
django-admin startproject clinic
  • Create a Django application called “booking”
python3 manage.py startapp booking

Step Two: Configuring project settings.py and urls.py files

  • In the clinic/clinic/settings.py file add your app name in the Installed apps section
INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','booking', #Added Django app name]
  • Next, we have to configure urls.py in clinic/clinic/urls.py
from django.contrib import adminfrom django.urls import path, includeurlpatterns = [path('admin/', admin.site.urls),path('', include("booking.urls")), #Added]

Note: Be sure to import “include” from django.urls

Step Three: Create a urls.py file in your booking folder

  • Add urls.py file to booking folder
urls.py file location

Step Four: Creating an Appointment Model in booking/models.py

  • In clinic/booking/models.py we have to create a Model named Appointment:
models.py file

As shown above, in each Appointment we collect 5 data:

1. The user booking this appointment

2. The selected service that the clinic provides

3. The selected day of the appointment

4. The selected time of the appointment

5. The time this appointment was ordered by the user

Don’t forget to add your Appointment Model in the booking/admin.py file

from django.contrib import adminfrom .models import *
admin.site.register(Appointment)

Step Five: Creating function-based views

  • In clinic/booking/views.py we are going to create some functions

For example, if the clinic is open on Mondays, Wednesdays, and Saturdays from 3 PM till 7:30 PM these will be the functions for the booking application:

views.py file

We have 12 functions and I’m going to explain what each of the 12 functions performs.

  1. The index() function only renders the template (index.html)
  2. The booking() function is the first step of the two-step booking process. first, it gets the next 21 valid days (Mondays, Wednesdays, and Saturdays) of the upcoming 21 days using the validWeekday() function which will be a total of 9 days. Then checks if any of those 9 days are full. After that, it shows the days in the template for the user to choose from. Then when the user posts his selected service and date of appointment it gets the data and stores it in the Django sessions and takes the user to the next step of the booking process.
  3. The bookinSubmit() function is the second step in the booking process. It gets the session data (service and day) then using the checkTime() function checks which time of the selected date is not booked. After that when the user selected his time it gets stored in the database.

Note: These if statements are just in case of any type of injection and it's not good to use this many if statements!

4. The userPanel() function displays the user’s booked appointments and allows the user to edit his appointments.

5. The userUpdate() function takes the id argument from the appointment selected to edit (update) and in addition to the booking() function it has a “delta24” variable which is to determine if the selected date is 24hrs before the day user’s in using datetime.today() function.

6. The userUpdateSubmit() function just like the bookingSubmit() function saves or in this case updates the appointment data. This function check’s its times slightly differently by using the checkEditTime() function instead of the checkTime() function and also the if statement is changed in line 154. These changes are so the user can pick his own selected time (the time he booked before) in case he just wants to change the service or the day of the appointment.

7. The staffPanel() function shows the bookings in the next 21 days in the template.

Note: Use the Django template tag as shown below in the staff template or set restrictions in views.py so only staff members can access the staff page.

{% if user.username == 'staff' %}<!-- Your HTML Code -->{% endif %}

8. The dayToWeekday() function takes an argument “x” (day) and converts it to a string so the Django template can show it to the user.

9. The validWeekday() function takes an argument “days” (the period you want to check the weekdays) and checks if each day in the period is Monday or Wednesday or Saturday then returns a list of “weekdays” of valid days.

10. The isWeekdayValid() function takes an argument “x ” and checks the list of days from validWeekday() if the days are full or not. Then returns a list of “validateWeekdays” of weekdays that are Monday, Wednesday, or Saturday and are not completely booked.

11. The checkTime() function takes two arguments “times” and “day” so it can check which times of that day are free to be booked by the user.

12. The checkEditTime() function is exactly like checkTime() but takes an additional argument “id” (the appointment’s id that the user is trying to edit) so the time of that appointment the user is editing would be shown to the user.

Step Six: Add URLs in urls.py file

  • In booking/urls.py file (which we created in step three) add these URLs:
urls.py file

You can check the templates in my Github repository

The templates use Bootstrap framework. If you are not familiar with Bootstrap check:

  • Also, I have added a members application for users to Sign-in/Sign-out/Sign-up which I created using this youtube video:

My YouTube Video Explaining This Booking System:

Summary

We managed to build a Django booking project in six steps using function-based views.

Remember there are other ways to implement this project this is one of them.

You can also implement CRUD functionality for staff members.

--

--