Small Project on user can work record after login :-
Project structure:-
Settings.py file code:-
ALLOWED_HOSTS = ['192.168.1.9']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
models.py file code:-
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import Permission
# Create your models here.
class customers(models.Model):
user=models.OneToOneField(User,primary_key=True,on_delete=models.CASCADE)
def __str__(self):
return self.user.username
class Tasks(models.Model):
Title=models.CharField(max_length=100)
Details=models.TextField()
Date=models.DateField(auto_now_add=True)
works=models.ForeignKey(customers,on_delete=models.CASCADE)
def __str__(self):
return self.Title
forms.py file code :-
from django import forms
from .models import customers,Tasks,User
from django.contrib.auth.forms import UserCreationForm,AuthenticationForm
class TaskForms(forms.ModelForm):
class Meta:
model=Tasks
fields=['Title','Details',]
widgets={
'Title': forms.TextInput(attrs={'class':'form-control'}),
'Details': forms.Textarea(attrs={'class':'form-control'}),
}
class CustomerForms(forms.ModelForm):
class Meta:
model=User
fields=['username','email',]
widgets={
'username': forms.TextInput(attrs={'class':'form-control'}),
'email': forms.TextInput(attrs={'class':'form-control'}),
}
class NewUserForm(UserCreationForm):
username=forms.CharField(max_length=100,
required=True,
widget=forms.TextInput(attrs={'placeholder': 'First Name',
'class': 'form-control',}))
password=forms.CharField(max_length=50,
required=True,
widget=forms.PasswordInput(attrs={'placeholder': 'Password',
'class': 'form-control',
'data-toggle': 'password',
'id': 'password',
}))
email=forms.EmailField(required=True,
widget=forms.TextInput(attrs={'placeholder': 'Email',
'class': 'form-control',
}))
class LoginForm(AuthenticationForm):
username = forms.CharField(max_length=100,
required=True,
widget=forms.TextInput(attrs={'placeholder': 'Username',
'class': 'form-control',
}))
password = forms.CharField(max_length=50,
required=True,
widget=forms.PasswordInput(attrs={'placeholder': 'Password',
'class': 'form-control',
'data-toggle': 'password',
'id': 'password',
'name': 'password',
}))
views.py file code :-
from django.shortcuts import render,redirect
from django.contrib.auth import login,logout,authenticate
from django.contrib.auth.forms import AuthenticationForm
from django .contrib.auth.models import User
from .forms import *
from django.contrib import messages
from .models import *
def homes(request):
return render(request, 'app1/base.html')
def AdminDashboard(request):
# users = User.objects.all().values()
Customers = customers.objects.all()
# data=users
return render(request, "app1/AdminDashboard.html", {"customers":Customers} )
def userViewDetails(request,pk):
tasks = Tasks.objects.filter(works_id=pk)
return render(request,'app1/userView.html',{'Tasks':tasks})
def home(request):
if request.user.is_superuser:
return redirect('admin-dash')
# else:
# return redirect('taskView')
customer=customers.objects.get(user=request.user)
tasks = Tasks.objects.filter(works_id = customer)
return render(request,'app1/userView.html',{'Tasks':tasks})
return render(request,'app1/home.html',{'details':customer})
def uploadTask(request):
customer=customers.objects.get(user_id=request.user.id)
form=TaskForms()
if request.method=="POST":
form=TaskForms(request.POST)
if form.is_valid():
Title=form.cleaned_data["Title"]
Details=form.cleaned_data["Details"]
model=Tasks(Title=Title,Details=Details,works=customer)
model.save()
form=TaskForms()
return redirect('taskView', customer.user_id)
else:
form = TaskForms()
return render (request,'app1/uploadTask.html',{'upload':form})
def updateTask(request,user_id,prod_id):
customer=customers.objects.get(user_id=user_id)
# if request.user.is_superuser:
# customer=customers.objects.get(user_id=)
abc=Tasks.objects.get(id=prod_id)
xyz=TaskForms(request.POST or None ,instance=abc)
if xyz.is_valid():
xyz.save()
return redirect('taskView',customer.user_id)
return render(request,'app1/uploadTask.html',{'upload':xyz})
def updateCustomer(request,user_id):
user=User.objects.get(id=user_id)
form=CustomerForms(request.POST or None ,instance=user)
if form.is_valid():
# username = form.cleaned_data["username"]
# email=form.cleaned_data["email"]
# model=User(username=username,email=email)
# model.save()
form.save()
return redirect('home')
return render(request,'app1/uploadCustomer.html',{'upload':form})
def login_req(request):
if request.method=="POST":
username=request.POST['username']
password=request.POST['password']
user=authenticate(username=username,password=password)
if user is not None:
login(request,user)
return redirect('home')
else:
messages.error(request,"invalid")
return redirect('login_req')
else:
form=LoginForm()
return render(request,"app1/login.html",{'login_form':form})
def AdminDelete(request,user_id):
# customer=customers.objects.get(user_id=user_id) #! Get Customer
user = User.objects.get(id=user_id)
user.delete()
return redirect('admin-dash')
def user_logout(request):
logout(request)
return redirect('/')
def register_req(request):
if request.method=="POST":
username=request.POST['username']
password=request.POST['password']
email=request.POST['email']
user=User.objects.create_user(username=username,password=password,email=email)
user.save()
customer=customers(user=user)
customer.save()
return redirect('login_req')
form =NewUserForm
return render(request,'app1/register.html',{'form':form})
def TaskDelete(request,user_id,prod_id):
# customer =customers.objects.get(user_id=user_id) #! Get Customer
task=Tasks.objects.get(id=prod_id)
task.delete()
return redirect('taskView',user_id)
urls.py file code :-
from django.contrib import admin
from django.urls import path
from app1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.homes,name='homes'),
path('login_req',views.login_req,name="login_req"),
path('admin-dash',views.AdminDashboard,name='admin-dash'),
path('taskView/<int:pk>',views.userViewDetails,name='taskView'),
path('home',views.home,name='home'),
path('uploadTask/',views.uploadTask,name='uploadTask'),
path(r'updatetask/<int:user_id>/<int:prod_id>/',views.updateTask,name='updatetask'),
path('updatecustomer/<int:user_id>',views.updateCustomer,name='updatecustomer'),
path('deletetask/<int:user_id>/<int:prod_id>/',views.TaskDelete,name='deletetask'),
path('deleteuser/<int:user_id>',views.AdminDelete, name='deleteuser'),
path('logout/',views.user_logout,name='logout'),
path('register_req',views.register_req,name="register_req"),
]
Templates/app1 folder inside your app folder(app1):-
userView.html file code :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>home page</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
<style>
#nav{
margin-bottom:20px;
display: flex;
justify-content: flex-end ;
gap:20px;
}
body{
margin: 0 20px;
}
</style>
</head>
<body>
<center ><h1>Welcome {{request.user}}</h1></center>
<div id='nav'>
{% if not request.user.is_superuser %}
<a class ="btn btn-success" href="{% url 'uploadTask' %}"> +</a>
{% endif %}
</div>
<table class="table table-striped table-dark">
<thead>
<tr>
<th scope="col">Title</th>
<th scope="col">Details</th>
<th scope="col">Date</th>
<th scope="col">Delete</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{% for task in Tasks %}
{% if request.user.is_superuser and forloop.first%}
<h5>Customer: {{task.works}}</h5>
{% endif %}
<tr>
<th scope="row">{{task.Title}}</th>
<td> {{task.Details}}</td>
<td>{{task.Date}}</td>
<td><a class="btn btn-danger" href="{% url 'deletetask' task.works.user.id task.id %}"> delete user</a></td>
<td><a class="btn btn-warning" href="{% url 'updatetask' task.works.user.id task.id %}"> update user</a></td>
</tr>
</tbody>
{% endfor %}
</table>
<a class="btn btn-dark" href="{% url 'logout' %}">Logout</a>
</html>
uploadTask.html file code :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>home page</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
<div class="form-content">
<form method="POST" enctype='multipart=data'>
{% csrf_token %}
<div class="form-row">
<div class="col-md-5">
<div class="form-group mt-3 mb-0">
<label class="small mb-1">Title:</label>
{{ upload.Title }}
<div class="col-md-6">
<div class="form-group">
<label class="small mb-1">Details:</label>
{{ upload.Details }}
</div>
</div>
<div class="form-group mt-4 mb-0">
<button type="submit" class="col-md-12 btn btn-dark">Submit</button><br><br>
</div>
register.html file code :-
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
</head>
<div class="form-content my-3 p-3">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-7">
<div class="card shadow-lg border-0 rounded-lg mt-0 mb-5">
<div class="card-header justify-content-center">
<h3 class="font-weight-light my-4 text-center">Create Account</h3>
</div>
<div class="card-body">
<form method="POST">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-dismissible" role="alert">
<div id="form_errors">
{% for key, value in form.errors.items %}
<strong>{{ value }}</strong>
{% endfor %}
</div>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endif %}
<div class="form-row">
<div class="col-md-6">
<div class="form-group">
{{ form.first_name }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{{ form.last_name }}
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6">
<div class="form-group">
<label class="small mb-1">Username</label>
{{ form.username }}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="small mb-1">Email</label>
{{ form.email }}
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6">
<div class="form-group">
<label class="small mb-1">Password</label>
{{ form.password }}
{{ form.errors.password }}
</div>
</div>
<div class="col-md-6">
</div>
</div>
</div>
<div class="form-group mt-4 mb-0">
<button type="submit" class="col-md-12 btn btn-dark">Sign Up</button><br><br>
</div>
</form>
</div>
<div class="card-footer text-center">
<div class="small">
<a href="login_req">Have an account? Go to Sign in</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
UploadCustomer.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.container {
margin-left: 550px;
margin-top: 150px;
width: 300px;
height: 100px;
padding: 15px;
}
<h1 style="text-align: center; padding-top: 1em; padding-bottom: 1em;"> UPdate Customers Information </h1>
</style>
</style>
<div class="container">
</head>
<body>
<table>
<tbody>
<form method="POST" enctype='multipart=data'>
{% csrf_token %}
{%for form in upload%}
{{ form.label}}
{{form}}
{% endfor %}
<div class="text-center">
<input class="btn btn-success" type="submit">
</div>
</tbody>
</table>
{% for key, value in form.errors.items %}
<strong>error{{ value }}</strong>
{% endfor %}
</div>
Login.html file code:-
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="form-content my-3 p-3">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-5">
<div class="card shadow-lg border-0 rounded-lg mt-0 mb-3">
<div class="card-header justify-content-center">
<h3 class="font-weight-light my-1 text-center">Sign In</h3>
</div>
{% if form.errors %}
<div class="alert alert-danger alert-dismissible" role="alert">
<div id="form_errors">
{% for key, value in form.errors.items %}
<strong>{{ value }}</strong>
{% endfor %}
</div>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
{% endif %}
<div class="card-body">
<form method="POST">
{% csrf_token %}
<div class="form-row">
<div class="col-md-10 offset-md-1">
<div class="form-group">
<a href="#"
class="btn btn-link btn-lg active btn-block">Sign in with GitHub</a>
<a href="#"
class="btn btn-link btn-lg active btn-block">Sign in with Google</a>
<hr>
<p class="text-center"><strong>OR</strong></p>
<hr>
<label class="small mb-1">Username</label>
{{ login_form.username }}
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-10 offset-md-1">
<div class="form-group">
<label class="small mb-1">Password</label>
{{ login_form.password }}
</div>
</div>
</div>
<div class="form-group mt-4 mb-0">
<button type="submit" class="col-md-12 btn btn-dark">Sign Up</button><br><br>
</div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-10 offset-md-1">
<div class="form-group mt-0 mb-1">
<button name="login" class="col-md-12 btn btn-dark" id="login">Sign in</button>
</div>
</div>
</div>
</form>
</div>
<div class="card-footer text-center">
<div class="small">
<a href= '/register_req' >Don't have an account yet? Go to signup</a><br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Home.html file code :-
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<table border="1">
<th>Customer name</th>
<th>Customer email</th>
{% for customer in details %}
<tr>
<td> <a href="{% url 'taskView' customer.user_id %}"> {{customer.user}}</a> </td>
<td>{{customer.user.email}}</td>
</tr>
{% endfor %}
</table>
base.html file code :-
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="jumbotron">
<h1 class="display-4">Welcome</h1>
<p class="lead">
This is <b>user registration and login system</b> build with django which is
a Python-based free and open-source web framework that follows the model–template–views architectural pattern.
</p>
<hr class="my-4">
<p class="lead">
<a class="btn btn-primary btn-lg" href="login_req" role="button">login</a>
<a class="btn btn-primary btn-lg" href="/register_req" role="button">Sign up</a>
</p>
</div>
Admindashboard.html file :-
app1
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<h1 style="text-align: center; padding-top: 1em; padding-bottom: 1em;">Customers Information</h1>
<div class="container mt-3">
<table class="table table-striped table-dark" >
<thead>
<tr>
<th>Index No</th>
<th>Customer</th>
<th>Email</th>
<th>Update</th>
<th>Delete</th>
</thead>
</tr>
<tbody>
{% for Customer in customers %}
<tr >
<th th scope="row">{{forloop.counter}}</th>
<td class="text-bg-dark"><a href="{% url 'taskView' Customer.user_id %}"> {{Customer.user}}</a>
<td>{{Customer.user.email}}</td>
<td><a href="{% url 'updatecustomer' Customer.user.id %}"> update</a></td>
{% comment %} {{Customer.user.id}} {% endcomment %}
<td><a href="{% url 'deleteuser' Customer.user.id %}">Delete</a></td>
</td>
</tr>
</tbody>
{% endfor %}
</table>
</div>
<div class="text-center">
<a href="/register_req"><button type="button" class="btn btn-primary">Add New Customer</button></a>
</div>
</body>
</table>
Sign up here with your email
ConversionConversion EmoticonEmoticon