Physician-Patient Appointment Application

A Physician-patient application that seeks to showcase the essential features of Django viz: views, URLs, forms, templates, models etc.

Step 1: Start the new Django Project and application.

Creating your first
To avoid redundancy, the link above guides you to the simple process of starting a new project and by extension application(s) in the Django framework.

In this case, let the name of our application be called appointment.

Step 2: Define the Models or Object Relational Mapper

In appointment/models.py

from django.db import models 
from django.contrib.auth.models import User

#Note the User model represents the users registered by the django superuser 

#Creating a model for the Physicians 
class Physician(models.Model):
    name=models.CharField(max_length=100)
    specialty=models.CharField(max_length=100)
    def __str__(self):
        return self.name #This ensures the name of the doctor appears when you have an inance of this model. 

#Creating a model for the Patients 
class Patient(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE) #The patient is assumed to be the users registered by the superuser at the admin interface
    phone_number=models.CharField(max_length=15)
    def __str__(self):
        return self.user.username
#Lets create the models for the appointment
class Appointment(models.Model):
    patient=models.ForeignKey(Patient,on_delete=models.CASCADE)
    appointment_date=models.DateTimeField()
    physician=models.ForeignKey(Physician,on_delete=models.CASCADE)
    def __str__(self):
        return f"Patient {self.patient.user.username} --{self.appointment_date}"
More on the models.py
This is abstraction at work, because you use similar or same codes irrespective of the database you are working with, which would have been defined in the settings.py file of the project folder.

Step 3: Create and apply Migrations.

python manage.py migrations appointment
python manage.py migrate
More on Create and apply migrations.
The first command i.e. python manage.py makemigrations appointment Django uses the command to track your migrations file and check for subtle changes in your application. The second command python manage.py migrate simply does the migration which effects the changes in your database schema.

Step 4: Setting up Forms.

In appointment/forms.py. You are to create the forms.py if it doesn't exist in the appointment app directory.

from django import forms 
from django.contrib.auth.forms import UserCreationForm
from .models import Patient,Appointment

#The PatientRegistration Form is extending the power of the UserCreationForm 
#already provided by Django by adding more field(s)
class PatientRegistrationForm(UserCreationForm):
    phone_number=forms.CharField(max_length=15,required=True)  
    class Meta:
        model=Patient
        fields=('username','password1','password2','phone_number')
class AppointmentForm(forms.ModelForm):
    class Meta:
        model=Appointment
        fields=('appointment_date','physician')
More on the forms.py
'UserCreationForm' is a built-in form class provided by Django in the 'django.contrib.auth.forms' module. It designed to simplify the process of creating a new user account.

Step 5: Setting up our Views.

In appointment/views.py

#Import neeeded libraries 
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login
from django.shortcuts import render,redirect
from django.views.generic import ListView
from .models import Physician,Patient,Appointment
from .forms import PatientRegistrationForm,AppointmentForm

#When called , returns the view essentially to make available the Physican 
#model in the template specified 
class PhysicianListView(ListView):
    model=Physician
    template_name='appointment/physicianlist.html')
class AppointmentListView(ListView):
    model=Appointment
    template_name='appointment/appointment_list.html'

@login_required 
#Only tries to protect the view, to ensure no access without due login
def patient_dashboard(request):
    appointments=Appointment.objects.filter(patient_user=request.user)
    return render(request,'appointment/patient_dashboard.html',{'appointments':appointments}
@login_required
def schedule_appointment(request):
    if request.method=="POST":
        form=AppointmentForm(request.POST)
        if form.is_valid():
            appointment=form.save(commit=False)
            appointment.patient=request.user.patient
            appointment.save()
            return redirect('patient_dashboard')
        else:
            form=AppointmentForm()
        return render(request,'appointment/patient_dashboard.html',{'form':form})
def register_patient(request):
    if request.method=='POST':
        form=PatientRegistrationForm(request.POST)
        if form.is_valid():
            user=form.save()
            login(request,user)
            return redirect('patient_dashboard')
        else:
            form=PatientRegistrationForm()
        return render(request,'appointment/register_patient.html',{'form':form})
def login_view(request):
    if request.method=='POST':
        form=AuthenticationForm(request,data=request.POST)
        if form.is_valid():
            user=form.get_user()
            login(request,user)
            return redirect('patient_dashboard')
        else:
            form=AuthenticationForm()
        return render(request,'appointment/login.html',{'form':form}
More on the views.py
The Import statements are used to obtain necessary modules and functions, and most of their names relates well to the functions they perform.

Step 6: Create Templates

You will create the following templates in the /templates/appointment folder.

  • 'physician_list.html'

  • 'appointment_list.html'

  • 'patient_dashboard.html'

  • 'schedule_appointment.html'

  • 'register_patient.html'

  • 'login.html'

Step 7: Setting up the URLs.

In appointment/urls.py

from django.urls import path
from .views import (
    PhysicianListView,
    AppointmentListView,
    patient_dashboard,
    schedule_appointment,
    register_patient,
    login_view
)
urlpatterns=[
    path('physicians',PhysicianListView.as_view(),name='physician_list'),
    path('appointments',AppointmentListView.as_view(),name='appointment_list'),
    path('patient/dashboard',patient_dashboard,name='patient_dashboard'),
    path('patient/schedule-appointment',schedule_appointment,name='schedule-appointment'),
    path('register-patient',register_patient,name='register_patient'),
    path('login/',login_view,name='login'),
]

Step 8: Configure Internalization

In settings.py of the Project folder

#...

LANGUAGE_CODE ='en-us'

LANGUAGES=[
    ('en',_('English')),
    ('es',_('Spanish')),
]

LOCALE_PATHS=[
    os.path.join(BASE_DIR,'locale'),
]

Step 9: Run the Development Server

python manage.py runserver

Feel free to comment, ask your questions and collaborate.

Follow me on the various social media handles

My Twitter handle

My Portfolio