Building a Simple HR Management System with Django: A Recap
Written on
Chapter 1: Project Overview
We have successfully developed a straightforward human resource management system (HRMS) tool. This project serves as an excellent starting point for developers venturing into Python, Django, and web development.
Project Highlights
Although HRMS v1 is relatively simple, there are several noteworthy aspects:
- Django MVT Architecture: This project utilizes the standard Model-View-Template (MVT) architecture. The model defines the data structure, the view manages the request handling and data processing, while the template structures the webpage and displays the data.
- PostgreSQL Database Interaction: The database operations have been encapsulated within a class that provides standard database methods.
- Executing Raw SQL Queries: The system supports executing raw SQL queries for database manipulation.
- Error Handling: We have implemented error handling for database operations.
Here's an example of how we connect to the PostgreSQL database using Python:
import psycopg2
class PSQLConn:
def __init__(self, conn_info):
self.conn_info = conn_info
self.result = [] # to store database results
self.flag = 0 # to indicate success (1) or failure (0)
self.msg = "" # to hold error messages
def get_db_data(self, sql):
"""
Retrieves data from the database using the provided SQL query.
"""
psql_conn = psycopg2.connect(
database=self.conn_info["NAME"],
host=self.conn_info["HOST"],
port=self.conn_info["PORT"],
user=self.conn_info["USER"],
password=self.conn_info["PASSWORD"],
)
try:
cursor = psql_conn.cursor()
cursor.execute(sql)
self.result = list(cursor.fetchall())
self.flag = 1
cursor.close()
psql_conn.close()
except Exception as e:
self.flag = 0
self.msg = str(e)
Connecting Frontend to Backend
Through form submissions and event handlers, we facilitate the interaction between the frontend and backend. We also leverage regular expressions for data validation prior to submission.
Here’s a snippet of jQuery that validates form fields:
$("#editform").submit(function () {
let name = $("#editform input[name='name']").val().trim();
if (name.length === 0) {
alert("Name cannot be empty");
return false;
}
// Gender validation
let gender = $("#editform select[name='gender']").val().trim();
if (!["Male", "Female", "Other"].includes(gender)) {
alert("Gender must be one of: Male, Female, or Other");
return false;
}
// Birthday validation
let birthday = $("#editform input[name='birthday']").val().trim();
if (!birthday.match(/^d{2}-d{2}-d{4}$/)) {
alert("Birthday must be in the format mm-dd-yyyy e.g., 05-31-2024.");
return false;
}
// Additional validations for mobile, email, and address...
return true;
});
Future Improvements
There are several areas for enhancement in upcoming versions:
- ORM (Object Relational Mapping): In future updates, we will replace raw SQL queries with ORM, which is a more elegant way to handle database interactions in Django.
- Ajax Implementation: To enhance user experience, we aim to integrate Ajax, allowing our HRMS to update content asynchronously without needing to reload the entire webpage.
Step by step, we are building something remarkable! Stay tuned for Progressive Django v2.
Thank you for your support!
We appreciate your involvement in the In Plain English community! Don't forget to clap and follow the writer!
Follow us on our social platforms: X | LinkedIn | YouTube | Discord | Newsletter. For more content, check out Stackademic | CoFeed | Venture | Cubed.
See more at PlainEnglish.io.
Chapter 2: Educational Videos
To further enhance your understanding of web development, we have included some insightful YouTube videos below.
The first video, "Web Development In 2023 - A Practical Guide," offers a thorough overview of the current landscape of web development, covering essential tools and frameworks.
The second video, "100 Days of Code - Web Development Bootcamp - Full Course: Day 1 - 8," provides a comprehensive guide through the initial stages of web development.