0% found this document useful (0 votes)
178 views

Django Curd

This document outlines the steps to create a basic CRUD (create, read, update, delete) application in Django. It includes initializing the project and app, defining a model with fields for employee data, creating forms to add and edit data, adding views to handle form submission and display data, and templates to render the forms and data tables. URLs are mapped to the views to navigate between the add, view, edit, and delete functions of the application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views

Django Curd

This document outlines the steps to create a basic CRUD (create, read, update, delete) application in Django. It includes initializing the project and app, defining a model with fields for employee data, creating forms to add and edit data, adding views to handle form submission and display data, and templates to render the forms and data tables. URLs are mapped to the views to navigate between the add, view, edit, and delete functions of the application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

django-admin startproject curd

python manage.py startapp myapp

//settings.py
'myapp',

python manage.py migrate

//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"

python manage.py migrate


python manage.py makemigrations

//form.py

from django import forms


from .models import Employee
class EmployeeForm(forms.ModelForm):
class Meta:
model = Employee
fields = "__all__"

//views.py

from django.shortcuts import render, redirect


from .forms import EmployeeForm
from .models import Employee
# Create your views here.

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})

def edit(request, id):


employee = Employee.objects.get(id=id)
return render(request,'edit.html', {'employee':employee})

def update(request, id):


employee = Employee.objects.get(id=id)
form = EmployeeForm(request.POST, instance = employee)
if form.is_valid():
form.save()
return redirect("/show")
return render(request, 'edit.html', {'employee': employee})

def destroy(request, id):


employee = Employee.objects.get(id=id)
employee.delete()
return redirect("/show")

create a folder templates

//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

from django.contrib import admin


from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('emp', views.emp),
path('show',views.show),
path('edit/<int:id>', views.edit),
path('update/<int:id>', views.update),
path('delete/<int:id>', views.destroy),
]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy