Step 1: Create the Django project and app
Open your terminal (or command prompt) and run the following commands:
# Create the Django project
django-admin startproject crud_project
cd crud_project
# Create a new app inside the project
python manage.py startapp crud_app
Step 2: Define the model and set up the database
Edit the models.py file in the crud_app directory:
# crud_app/models.pyfrom django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
email = models.EmailField()
def __str__(self):
return self.name
After defining the model, run migrations to create the database table:
python manage.py makemigrations
python manage.py migrate
Step 3: Create views for add, update, and delete operations
Edit the views.py file in the crud_app directory:
# crud_app/views.pyfrom django.shortcuts import render, redirect, get_object_or_404
from .models import MyModel
from .forms import MyModelForm
def add_data(request):
if request.method == 'POST':
form = MyModelForm(request.POST)
if form.is_valid():
form.save()
return redirect('show_data')
else:
form = MyModelForm()
return render(request, 'add_data.html', {'form': form})
def update_data(request, pk):
obj = get_object_or_404(MyModel, pk=pk)
if request.method == 'POST':
form = MyModelForm(request.POST, instance=obj)
if form.is_valid():
form.save()
return redirect('show_data')
else:
form = MyModelForm(instance=obj)
return render(request, 'update_data.html', {'form': form})
def delete_data(request, pk):
obj = get_object_or_404(MyModel, pk=pk)
if request.method == 'POST':
obj.delete()
return redirect('show_data')
return render(request, 'delete_data.html', {'obj': obj})
Step 4: Create forms for the model
Create a new file named forms.py in the crud_app directory to define the form for the model:
# crud_app/forms.pyfrom django import forms
from .models import MyModel
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('name', 'age', 'email')
Step 5: Create templates for add, update, and delete operations
Create templates for the views we defined earlier.
a. add_data.html:
<!-- crud_app/templates/add_data.html --><!DOCTYPE html><html><head><title>Add Data</title></head><body><h1>Add Data</h1><form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save"></form></body></html>
b. update_data.html:
<!-- crud_app/templates/update_data.html --><!DOCTYPE html><html><head><title>Update Data</title></head><body><h1>Update Data</h1><form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save"></form></body></html>
c. delete_data.html:
<!-- crud_app/templates/delete_data.html --><!DOCTYPE html><html><head><title>Delete Data</title></head><body><h1>Delete Data</h1><p>Are you sure you want to delete {{ obj.name }}?</p><form method="post">
{% csrf_token %}
<input type="submit" value="Delete"></form></body></html>
Step 6: Create URL patterns
Edit the urls.py file in the crud_project directory:
# crud_project/urls.pyfrom django.urls import path
from crud_app import views
urlpatterns = [
path('add/', views.add_data, name='add_data'),
path('update/<int:pk>/', views.update_data, name='update_data'),
path('delete/<int:pk>/', views.delete_data, name='delete_data'),
path('show/', views.show_data, name='show_data'), # Add this if you haven't done it before
]
Step 7: Run the development server
Now, start the development server using the following command:
python manage.py runserver
Visit http://127.0.0.1:8000/add/ in your browser to add new data. To update and delete data, visit the corresponding URLs as specified in the URL patterns (e.g., http://127.0.0.1:8000/update/1/ to update data with primary key 1, and http://127.0.0.1:8000/delete/1/ to delete data with primary key 1).
That's it! You now have a Django project that allows you to add, update, and delete data using Django's ORM system.
Comments