School TimeTable Generation in Python/Django


In this blog, you can see how to generate a timetable for a school in Python/Django. In this web application you can also generate, delete and show the school time table. Also you can search the time table with respect to the teacher or class or day. 

First create the project by using the following command and then go to that project directory. You can write the following commands to do that. 

django-admin startproject TimeTables
cd TimeTables

After that create the Django app by using the following command.

python manage.py startapp timetable

After that put the app name into "settings.py" file like in the following image.

.

After that put the following code in "TimeTables --> urls.py"

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('timetable/', include('timetable.urls'))
]

After that put the following code in "timetable --> views.py" 

import random
from .models import *
from django.shortcuts import render
from django.http import HttpResponse
from .models import Teachers, Class, Day,Data
from .models import Data


# Create your views here.
def index(request):
classes = Class.objects.all()
teachers = Teachers.objects.all()
day =Day.objects.all()
data = []
total_c=Class.objects.all().count()
total_d=Day.objects.all().count()
total_l=Lecture.objects.all().count()
total=total_c*total_d*total_l
for x in range(total):
randIndex = random.choice(teachers)
data.append(randIndex)
total_t=Lecture.objects.all().count()*Day.objects.all().count()
if(total_c==3):
for x in range(0,total_t,1):
while (data[x] == data[x + total_t] or data[x] == data[x + total_t*2]):
randIndex = random.choice(teachers)
data[x] = (randIndex)
for x in range(total_t, total_t*2, 1):
while (data[x] == data[x + total_t]):
randIndex = random.choice(teachers)
data[x] = (randIndex)

if (total_c == 4):
for x in range(0, total_t, 1):
while (data[x] == data[x + total_t] or data[x] == data[x + total_t * 2]or data[x] == data[x + total_t * 3]):
randIndex = random.choice(teachers)
data[x] = (randIndex)
for x in range(total_t, total_t*2, 1):
while (data[x] == data[x + total_t]or data[x] == data[x + total_t*2]):
randIndex = random.choice(teachers)
data[x] = (randIndex)
for x in range(total_t*2, total_t*3, 1):
while (data[x] == data[x + total_t]):
randIndex = random.choice(teachers)
data[x] = (randIndex)

if (total_c == 5):
for x in range(0, total_t, 1):
while (data[x] == data[x + total_t] or data[x] == data[x + total_t * 2]or data[x] == data[x + total_t * 3]or data[x] == data[x + total_t * 4]):
randIndex = random.choice(teachers)
data[x] = (randIndex)
for x in range(total_t, total_t * 2, 1):
while (data[x] == data[x + total_t] or data[x] == data[x + total_t * 2]or data[x] == data[x + total_t * 3]):
randIndex = random.choice(teachers)
data[x] = (randIndex)
for x in range(total_t * 2, total_t * 3, 1):
while (data[x] == data[x + total_t]or data[x] == data[x + total_t*2]):
randIndex = random.choice(teachers)
data[x] = (randIndex)
for x in range(total_t*3, total_t*4, 1):
while (data[x] == data[x + total_t]):
randIndex = random.choice(teachers)
data[x] = (randIndex)

f = 0
count = 7
#cls = Data.objects.values_list('classes', flat=True)
for x in range(0, len(classes), 1):
count+=1
for y in range(0, len(day), 1):
classes_info=count
day_info=day[y]
for z in range(0, (total_l), 1):
if(z==0):
lecture_one=data[f]
if (z == 1):
lecture_two=data[f]
if (z == 2):
lecture_three=data[f]
if (z == 3):
lecture_four=data[f]
if (z == 4):
lecture_five=data[f]
f+=1
if(total_l==5):
timetable=Data(classes=classes_info,day=day_info,lecture_one=lecture_one,lecture_two=lecture_two,lecture_three=lecture_three,lecture_four=lecture_four,lecture_five=lecture_five)
timetable.save()
if (total_l == 4):
timetable = Data(classes=classes_info, day=day_info, lecture_one=lecture_one, lecture_two=lecture_two,
lecture_three=lecture_three, lecture_four=lecture_four, lecture_five="")
timetable.save()
if (total_l == 3):
timetable = Data(classes=classes_info, day=day_info, lecture_one=lecture_one, lecture_two=lecture_two,
lecture_three=lecture_three, lecture_four="", lecture_five="")
timetable.save()
if (total_l == 2):
timetable = Data(classes=classes_info, day=day_info, lecture_one=lecture_one, lecture_two=lecture_two,
lecture_three="", lecture_four="", lecture_five="")
timetable.save()
if (total_l == 1):
timetable = Data(classes=classes_info, day=day_info, lecture_one=lecture_one, lecture_two="",
lecture_three="", lecture_four="", lecture_five="")
timetable.save()


return render(request,'timetable/index.html')



def info(requets):
obj = Data.objects.all()

return render(requets,'timetable/basic.html',{"obj":obj})



def main(request):
return render(request,'timetable/main.html')


def delete(request):
obj = Data.objects.all()
obj.delete()
return render(request,'timetable/delete.html')



def search(request):
return render(request,'timetable/search.html')


def find(request):
data=request.GET.get('q','default')
obj = Data.objects.filter(classes=data)
return render(request,'timetable/basic.html',{"obj":obj})

def find_teacher(request):
data=request.GET.get('q','default')
datas = request.GET.get('d', 'default')
obj_o = Data.objects.filter(lecture_one=data,day=datas)
obj_t = Data.objects.filter(lecture_two=data,day=datas)
obj_th = Data.objects.filter(lecture_three=data,day=datas)
obj_f = Data.objects.filter(lecture_four=data,day=datas)
obj_fi = Data.objects.filter(lecture_five=data,day=datas)
params={
"obj_one":obj_o,
"obj_two":obj_t,
"obj_three":obj_th,
"obj_four":obj_f,
"obj_five":obj_fi,
}
return render(request,'timetable/teacherbasic.html',params)

def find_day(request):
data=request.GET.get('q','default')
obj = Data.objects.filter(day=data)
params={
"obj":obj,
}
return render(request,'timetable/basic.html',params)

After that put the following code into the "timetable --> urls.py"

from django.urls import path
from . import views

urlpatterns=[
path('generate/', views.index, name="index"),
path('delete/', views.delete, name="delete"),
path('full/', views.info, name="info"),
path('search/', views.search, name="search"),
path('find/', views.find, name="find"),
path('find_teacher/', views.find_teacher, name="find_teacher"),
path('find_day/', views.find_day, name="find_day"),
path('', views.main, name="main")

]

After that putthe following code into the "timetable --> models.py"

from django.db import models


# Create your models here.
class Teachers(models.Model):
id = models.AutoField,
name = models.CharField(max_length=20)

def __str__(self):
return self.name


class Class(models.Model):
id=models.AutoField,
name=models.CharField(max_length=2)

def __str__(self):
return self.name


class Day(models.Model):
id=models.AutoField,
name=models.CharField(max_length=10)

def __str__(self):
return self.name

class Data(models.Model):
id=models.AutoField,
classes=models.CharField(max_length=5,default="")
day=models.CharField(max_length=10, default="")
lecture_one=models.CharField(max_length=20,default="")
lecture_two=models.CharField(max_length=20,default="")
lecture_three=models.CharField(max_length=20,default="")
lecture_four=models.CharField(max_length=20,default="")
lecture_five=models.CharField(max_length=20,default="")



class Lecture(models.Model):
id=models.AutoField,
name=models.CharField(max_length=2)

def __str__(self):
return self.name

After that put the following code into "timetable --> admin.py"

from django.contrib import admin
from .models import Teachers, Class, Data, Lecture
from .models import Day

# Register your models here.
admin.site.register(Teachers)
admin.site.register(Class)
admin.site.register(Day)
admin.site.register(Data)
admin.site.register(Lecture)

After that put the following code intp "timetable --> apps.py"

from django.apps import AppConfig
class TimetableConfig(AppConfig):

    name = 'timetable' 

By writing this code you can generate the time table and can search the time table for a specific teacher or class. If you want the complete code then contact us. 


For Complete Code Contact Us









avatar
AUTHOR: Muhammad Abbas
I am Software Engineer.

2 Comments
avatar

Hello!!! can you help me complete source code ,,,,,,

Replay
avatar

Hey, can you please share the complete code?

Replay