Building A Hostel Managing System With Django

Naman Tiwari
Nybles
Published in
6 min readJul 5, 2018
Related image

This post serves as an archive of the work done and a guide for the rookie Django developers or anyone who wants to get a taste of development with Django. If you are an absolute beginner then I would suggest to go through this tutorial series first.

In the summer of 2018, the student members of the Software Wing of Geekhaven, the technical society of IIIT Allahabad came up with the idea to make a website to automate and simplify everything related to the hostels of our college. We decided to use Django for back-end and simple HTML/CSS for front-end. The aim of the project is to completely automate the process of allotting rooms to students. The students will not have to go through the hassle of reporting at the hostel or fight their way to find out which rooms are vacant at the caretaker’s office anymore. Now they will be able to select a room by logging into the online portal.

Why Django?

We decided to prefer Django over Javascript for this project simply because of its simplicity and the fact that Django is slightly better for data-driven websites.

Django Architecture

Django follows a Model-View-Controller(MVC) architecture, which is split up into three different parts:

  • The Model is the logical data structure behind the entire application and is represented by a database.
  • The View is the user interface — what you see in your browser when you visit a website. These are represented by HTML/CSS/Javascript files.
  • The Controller is the middleman that connects the view and model together, meaning that it is the one passing data from the model to the view.

So say a user will enter a URL in their browser, that request will go through the internet protocols, to your server, which will then call Django. Django will process the given URL path, and if it matches an URL path you have explicitly stated in the urls.py file, it will call the Controller, which will then perform a certain action, such as get an entry from your Model (database) and then render a View (it may be JSON text or HTML/CSS/JavaScript Web page).

Django is, however, also called a Model-View-Template (MVT) framework. In fact the main difference between the two patterns is that Django itself takes care of the Controller part (Software Code that controls the interactions between the Model and View), leaving us with the template. The template is a HTML file mixed with Django Template Language (DTL).

Folder Structure

You can find all the code in this repository. The folder structure is as follows -

Hostel-management-system
├── selection
│ ├── migrations
│ ├── templates
│ │ └── ...
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── forms.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── hms
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py

Designing the models

(Github models.py)

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. Learn more about making Django models here.

We made four models, ‘Student’, ‘Room’, ‘Hostel’ and ‘Course’. The ‘Student’ model stores the information of the student and is connected to the user using the OnetoOneField in Django. It stores all the information associated with the student. This is what the code looks like -

A Room Model is made which stores each vacant space separately (i.e. Room 5000_L and 5000_R). We decided to store each double room as two separate rooms that have the same number but different ‘names’ (for example a valid room name is 5000_L then the corresponding room number is 5000). This makes it easier to have double rooms. There are two entries of each double room present in the database.

The Room model

A Hostel Model which stores the details of the hostels. The challenge we faced here was that in our college some hostels are accommodated by students of different courses. Fortunately Django allows many to many relationship between objects so in the Hostel model we store a list of Foreign Keys of all the courses that are residing in the hostel. It uses Django’s ManytoManyField. It helps to choose the hostels which are available to a student based on his course.

The Hostel model

A Course Model which stores the course code for each student. It gives information about the semester and course the student is currently pursuing, we store it in the form of a code, i.e. If a student has enrollment number “IIT2017034”, his course code will be “IIT2017”. It also contains the room_type that is allotted to the course.

Making Forms For Our Models

(Github - forms.py)

Django forms are simple forms that can be used to take input from user in a view and create changes to our models. Learn more about Django forms here

Making forms for models in Django is easy. It can be easily done by using ModelForm (see the RegistrationForm below). The ModelForm allows to create fields for models by simply selecting the model and adding its fields in the fields list. The RegistrationForm is made to create a new Student Object and store its details and the SelectionForm is used to select room for the student. The UserForm is created using the UserCreationForm which can be imported from django.contrib.auth.forms. The Userform can be used to create a new user and LoginForm to login an existing user.

Creating The URLs and Views

(Github - views.py, urls.py)

A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. Learn more about urls here and then views here.

Whenever a URL is requested Django tries to find a match in the urls.py file and once a match is found it sends the request to a View. For example in the code given below, which shows the basic working of the website only. when the user will open the URL of the website Django opens the home.html. The home.html file must be present in the selection/templates folder and the settings.py must have the location of the templates. More information on the templates is given in the next section.

# The template folder's location must be mentioned in the settings.pyTEMPLATES = [
{
'BACKEND':'django.template.backends.django.DjangoTemplates',
'DIRS': ['selection/templates'],
'APP_DIRS': True,
}
]

The code for urls.py and views.py is given below.

urls.py
views.py

The Front End

What’s a template? A template is a file that we can re-use to present different information in a consistent format — for example, you could use a template to help you write a letter because although each letter might contain a different message and be addressed to a different person, they will share the same format. In Django, templates are reusable HTML pages. Learn more about them here

The main page

The site is mobile friendly and uses no Javascript. It has a simple flat design and no external dependency. Everything uses custom CSS present within the base.html template. We also implemented a graphical user interface that has a map of the entire hostel just like the seat matrix of the cinema halls in movie ticket booking websites like book-my-show. However, we still have to add the layout for each hostel.

Matrix View for Hostels and Mobile view

Final Words

Feel free to use the code in github for learning or creating something yourself. And that’s all Folks! Feel free to leave a comment for any doubts or a quick discussion.

--

--