Django Curd
Django Curd
Django Curd
//settings.py
'myapp',
//model.py
from django.db import models
class Employee(models.Model):
ename = models.CharField(max_length=100)
eemail = models.EmailField()
econtact = models.CharField(max_length=15)
class Meta:
db_table = "employee"
//form.py
//views.py
def emp(request):
if request.method == "POST":
form = EmployeeForm(request.POST)
if form.is_valid():
try:
form.save()
return redirect('/show')
except:
pass
else:
form = EmployeeForm()
return render(request,'index.html',{'form':form})
def show(request):
employees = Employee.objects.all()
return render(request,"show.html",{'employees':employees})
//index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<form method="POST" action="/emp">
{% csrf_token %}
<br>
<h3>Enter Details</h3>
Employee Name:
{{ form.ename }} <br>
Employee Email:
{{ form.eemail }} <br>
Employee Contact:
{{ form.econtact }}<br>
<button type="submit">Submit</button>
</form>
</body>
</html>
//edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<form method="POST" action="/update/{{employee.id}}">
{% csrf_token %}
<h3>Update Details</h3>
Employee Name:
<input type="text" name="ename" id="id_ename" required
maxlength="100" value="{{ employee.ename }}" />
<br>
Employee Email:
<input type="email" name="eemail" id="id_eemail" required
maxlength="254" value="{{ employee.eemail }}" />
<br>
Employee Contact:
<input type="text" name="econtact" id="id_econtact" required
maxlength="15" value="{{ employee.econtact }}" />
<button type="submit" class="btn btn-success">Update</button>
</form>
</body>
</html>
//show.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Employee Records</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Email</th>
<th>Employee Contact</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for employee in employees %}
<tr>
<td>{{ employee.id }}</td>
<td>{{ employee.ename }}</td>
<td>{{ employee.eemail }}</td>
<td>{{ employee.econtact }}</td>
<td>
<a href="/edit/{{ employee.id }}">Edit</a>
<a href="/delete/{{ employee.id }}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<br>
<br>
<center><a href="/emp">Add New Record</a></center>
</body>
</html>
//url.py