Ajp
Ajp
OUTPUT-
Office Employee Management System-
Add An Employee-
Filter An Employee Details-
CODE-
Setting.py-
""" # Build paths inside the project like this:
BASE_DIR / 'subdir'.
Django settings for office_emp_proj project.
import os
BASE_DIR =
Generated by 'django-admin startproject' using Path(__file__).resolve().parent.parent
Django 5.0.6.
TEMP_DIR=os.path.join(BASE_DIR,'template')
ALLOWED_HOSTS = [] 'django.middleware.clickjacking.XFrameOption
sMiddleware',
]
# Application definition
ROOT_URLCONF = 'office_emp_proj.urls'
INSTALLED_APPS = [
TEMPLATES = [
'django.contrib.admin',
{
'django.contrib.auth',
'BACKEND':
'django.contrib.contenttypes', 'django.template.backends.django.DjangoTempl
'django.contrib.sessions', ates',
'emp_app' 'OPTIONS': {
] 'context_processors': [
'django.template.context_processors.debug',
MIDDLEWARE = [
'django.template.context_processors.request',
'django.middleware.security.SecurityMiddleware
',
'django.contrib.auth.context_processors.auth',
'django.contrib.sessions.middleware.SessionMid
dleware', 'django.contrib.messages.context_processors.me
ssages',
'django.middleware.common.CommonMiddlewa ],
re', },
}, 'NAME':
'django.contrib.auth.password_validation.Minim
]
umLengthValidator',
},
WSGI_APPLICATION = {
'office_emp_proj.wsgi.application'
'NAME':
'django.contrib.auth.password_validation.Comm
onPasswordValidator',
# Database },
# {
https://docs.djangoproject.com/en/5.0/ref/setting 'NAME':
s/#databases 'django.contrib.auth.password_validation.Numer
icPasswordValidator',
DATABASES = { },
'default': { ]
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
} # Internationalization
} #
https://docs.djangoproject.com/en/5.0/topics/i18
n/
{
'NAME': USE_TZ = True
'django.contrib.auth.password_validation.UserAt
tributeSimilarityValidator',
},
# Static files (CSS, JavaScript, Images)
{
#
https://docs.djangoproject.com/en/5.0/howto/stat name=models.CharField(max_length=100,null=
ic-files/ False)
dept=models.ForeignKey(Department,on_delete
=models.CASCADE)
Models.py salary=models.IntegerField(default=0)
role=models.ForeignKey(Role,on_delete=model
# Create your models here . s.CASCADE)
class Department(models.Model): phone=models.IntegerField(default=0)
hire_date=models.DateField()
name=models.CharField(max_length=100,null=
False)
def __str__(self):
locations=models.CharField(max_length=100)
return "%s %s %s
"%(self.first_name,self.last_name,self.phone)
def __str__(self):
return self.name
Admin.py-
from django.contrib import admin
class Role(models.Model): from . models import
Employee,Role,Department
# Register your models here. from emp_app import views
admin.site.register(Employee)
admin.site.register(Role) urlpatterns = [
admin.site.register(Department) path('',views.index,name='index'),
path('all_emp', views.all_emp,
name='all_emp'),
path('add_emp', views.add_emp,
Urls.py- name='add_emp'),
"""
path('remove_emp',views.remove_emp,name='re
URL configuration for office_emp_proj project. move_emp'),
path('remove_emp/<int:emp_id>',
The `urlpatterns` list routes URLs to views. For views.remove_emp, name='remove_emp'),
more information please see: path('filter_emp', views.filter_emp,
name='filter_emp'),
https://docs.djangoproject.com/en/5.0/topics/http ]
/urls/
Examples:
Function views
Views.py
1. Add an import: from my_app import views
from django.shortcuts import
2. Add a URL to urlpatterns: path('',
render,HttpResponse
views.home, name='home')
from .models import Employee ,Role,
Class-based views
Department
1. Add an import: from other_app.views
from datetime import datetime
import Home
from django.db.models import Q
2. Add a URL to urlpatterns: path('',
Home.as_view(), name='home') # Create your views here.
Including another URLconf def index(request):
1. Import the include() function: from return render(request,'index.html')
django.urls import include, path
2. Add a URL to urlpatterns: path('blog/',
include('blog.urls')) def all_emp(request):
""" emps=Employee.objects.all()
if request.method=="POST":
first_name=request.POST['first_name'] except:
last_name = request.POST['last_name']
salary = int(request.POST['salary']) return HttpResponse("Please Enter A
Valid EMP ID")
bonus = int(request.POST['bonus'])
emps = Employee.objects.all()
phone = int(request.POST['phone'])
context={
dept = int(request.POST['dept'])
'emps':emps
role = int(request.POST['role'])
}
new_emp=Employee(first_name=first_name,last return
_name=last_name,salary=salary,bonus=bonus,ph render(request,'remove_emp.html',context)
one=phone,dept_id=dept,role_id=role,hire_date
=datetime.now())
def filter_emp(request):
new_emp.save()
if request.method=="POST":
return HttpResponse('Employee added
Successfully') name = request.POST['name']
elif request.method=="GET": dept =request . POST['dept']
return render(request,'add_emp.html') role =request. POST['role']
else: emps=Employee.objects.all()
return HttpResponse('An Exception
Occured! Emplyoee Has Not Been Added')
if name:
if emp_id:
if dept:
<meta charset="utf-8">
emps=emps.filter(dept__name__icontains=dept)
<meta name="viewport"
content="width=device-width, initial-scale=1">
if role: <title>Office Employee Management</title>
<link
emps=emps.filter(role__name__icontains=role) href="https://cdn.jsdelivr.net/npm/bootstrap@5.
3.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-
4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQ
context={ NGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
crossorigin="anonymous">
<style>
'emps':emps
.container-fluid{
background-color: #00d6c2;
}
}
return
render(request,'view_all.html',context)
#bg{
background-color: aqua;
elif request.method=='GET': }
else:
return HttpResponse('An Exception <body id="bg">
Occurred!') <div class="container">
<header>
<h1>OFFICE EMPLOYEE
Index.html- MANAGEMENT SYSTEM</h1>
<hr>
<!doctype html>
<nav class="navbar navbar-expand-lg
<html lang="en">
bg-body-tertiary" style="background-color:
pink">
<a class="nav-link"
href="/add_emp" role="button">Add An <script
Employee</a> src="https://cdn.jsdelivr.net/npm/[email protected].
</li> 1/dist/js/bootstrap.bundle.min.js"
</li> </body>
<li class="nav-item">
</html> }
</style>
</head>
Add_emp.html-
<!DOCTYPE html> <body id="bg">
<link {% csrf_token %}
href="https://cdn.jsdelivr.net/npm/bootstrap@5.
3.1/dist/css/bootstrap.min.css" rel="stylesheet"
<label for="first_name">First
integrity="sha384- Name</label><br>
4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQ
NGCHeKRQN+PtmoHDEXuppvnDJzQIu9" <input type="text" id="first_name"
crossorigin="anonymous"> name="first_name"
value="{{first_name}}"><br>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]. <label for="last_name">Last
1/dist/js/bootstrap.bundle.min.js" Name</label><br>
</body>
#bg{
</html>
background-color: aqua;
}
</style>
Filter_emp.html- </head>
<!DOCTYPE html>
<html lang="en"> <body id="bg">
<div class="container">
<head> <!-- Content here -->
<meta charset="UTF-8"> <h1>Filter An Emplyoee Details!</h1>
<title>Filter Employee</title> <hr>
<link <form action="/filter_emp"
href="https://cdn.jsdelivr.net/npm/bootstrap@5. method="post">
3.1/dist/css/bootstrap.min.css" rel="stylesheet"
{% csrf_token %}
integrity="sha384-
4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQ
NGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
<label for="name">Employee First/Last
crossorigin="anonymous">
Name</label><br>
<script
<input type="text" id="name"
src="https://cdn.jsdelivr.net/npm/[email protected].
name="name" value="{{name}}"><br>
1/dist/js/bootstrap.bundle.min.js"
<label <script
for="dept">Department</label><br> src="https://cdn.jsdelivr.net/npm/[email protected].
1/dist/js/bootstrap.bundle.min.js"
<input type="text" id="dept"
name="dept" value="{{dept}}"><br> integrity="sha384-
HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBN
<label for="role">Role</label><br> S5n7C8IVInixGAoxmnlMuBnhbgrkm"
<input type="text" id="role" crossorigin="anonymous"></script>
name="role" value="{{role}}"><br>
<style>
<hr>
.container{
<button type="submit" class="btn btn-
primary">Submit</button> background-color: #00d6c2;
</form> height: 200px;
</div> padding: 10px;
}
</body>
#bg{
</html>
background-color: aqua;
}
Remove_emp.html- </style>
mon </head>
<!DOCTYPE html>
<html lang="en"> <body id="bg">
<div class="container">
<head>
<meta charset="UTF-8"> <h1>Remove An Employee! </h1>
<!DOCTYPE html>
<html lang="en"> <body id="bg">
<div class="container">