Django Tutorial #7: Understanding CRUD Operations in Django | Create, Read, Update and Delete
Welcome to Part 7 of my Django Tutorial Series! 🚀
In the previous tutorials, we explored Django Models, Databases, Templates, and Forms.
Now it's time to understand one of the most important concepts in database-driven applications: CRUD Operations.
Almost every application needs to manage information. For example, a student management system needs to add new students, display student records, update details, and delete records when required.
This is exactly what CRUD operations help us achieve.
In this tutorial, we will learn:
- What are CRUD operations?
- What does CRUD stand for?
- How to create records.
- How to read database records.
- How to update existing records.
- How to delete records.
- A practical Django example.
1. What Are CRUD Operations?
CRUD stands for:
- C – Create: Add new records.
- R – Read: Retrieve and display records.
- U – Update: Modify existing records.
- D – Delete: Remove records.
These four operations form the foundation of many database-driven applications.
2. Preparing a Django Model
First, let's create a simple Student model.
models.py
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
roll_number = models.IntegerField()
def __str__(self):
return self.name
Run the migration commands:
python manage.py makemigrations
python manage.py migrate
3. Create Operation
The Create operation is used to insert new records into the database.
views.py
from django.shortcuts import render
from .models import Student
def add_student(request):
if request.method == 'POST':
Student.objects.create(
name=request.POST['name'],
email=request.POST['email'],
roll_number=request.POST['roll_number']
)
return render(request, 'add_student.html')
add_student.html
<h2>Add Student</h2>
<form method="POST">
{% csrf_token %}
<input type="text" name="name"
placeholder="Enter Name" required>
<input type="email" name="email"
placeholder="Enter Email" required>
<input type="number" name="roll_number"
placeholder="Enter Roll Number" required>
<button type="submit">Save Student</button>
</form>
This form collects student information and creates a new database record.
4. Read Operation
The Read operation retrieves records from the database.
views.py
def student_list(request):
students = Student.objects.all()
return render(
request,
'student_list.html',
{'students': students}
)
student_list.html
<h2>Student Records</h2>
{% for student in students %}
<p>{{ student.name }}</p>
<p>{{ student.email }}</p>
<p>{{ student.roll_number }}</p>
<hr>
{% endfor %}
This displays all available student records.
5. Update Operation
The Update operation modifies an existing database record.
views.py
from django.shortcuts import get_object_or_404, redirect
def update_student(request, id):
student = get_object_or_404(Student, id=id)
if request.method == 'POST':
student.name = request.POST['name']
student.email = request.POST['email']
student.roll_number = request.POST['roll_number']
student.save()
return redirect('student_list')
return render(
request,
'update_student.html',
{'student': student}
)
This retrieves the selected student, updates the entered information, and saves the changes.
6. Delete Operation
The Delete operation removes a record from the database.
views.py
def delete_student(request, id):
student = get_object_or_404(Student, id=id)
if request.method == 'POST':
student.delete()
return redirect('student_list')
This deletes the selected student when the request is submitted using POST.
7. Connecting URLs
Open urls.py and add the following URL patterns:
from django.urls import path
from . import views
urlpatterns = [
path('add/', views.add_student, name='add_student'),
path('students/', views.student_list, name='student_list'),
path('update/<int:id>/', views.update_student, name='update_student'),
path('delete/<int:id>/', views.delete_student, name='delete_student'),
]
Now all four operations are connected to their respective views.
8. Understanding CRUD with an Example
Imagine a student management application:
| Operation | Example |
|---|---|
| Create | Add a new student |
| Read | View student details |
| Update | Change a student's email |
| Delete | Remove a student record |
These operations help applications manage information efficiently.
9. Why Are CRUD Operations Important?
- They provide complete data management functionality.
- They make applications interactive.
- They allow users to manage database records.
- They are used in management systems and e-commerce applications.
- They form the foundation of many web applications and APIs.
10. Conclusion
CRUD Operations are among the most important concepts in Django development.
By understanding Create, Read, Update, and Delete, developers can build applications that manage information efficiently.
These concepts are useful in student management systems, employee management systems, inventory applications, and many other real-world projects.
This completes another important stage of my Django learning journey!
What's Next?
In the upcoming tutorials, I plan to explore Django Authentication, User Login and Registration, and other advanced Django concepts.
If you are also learning Django, let me know what you are currently building!
Happy Coding! 💜
— SP Randhawa
Top comments (1)
🚀 Welcome to Part 7 of my Django Tutorial Series!
In this tutorial, we explored CRUD Operations:
✅ Create
✅ Read
✅ Update
✅ Delete
These four operations are the foundation of many database-driven applications.
💡 What kind of project would you like to build using CRUD operations?
Share your ideas below. Let's learn and grow together!
Happy Coding! 💜