Saturday, November 28, 2020
Home > django > Django CRUD Application | Make CRUD application

Django CRUD Application | Make CRUD application

Django CRUD application

CRUD means Create Read Update Delete. This is a very very important topic. Whenever you create an application, you must know how to make CRUD.

Create, Read, Update, Delete are four basic functions of any software or web application. CRUD paradigm is most common in web application.

For example, Imagine you are making a library app, you have created a database. so you don’t want to add manually in database. You don’t want to update or delete by going to database table and press delete there. So you want to make a platform where you can create entry for books. Like a form. You will enter everything about books, like book name, isbn number, book author.

After creating, now you want to view all the books and their details. After viewing, you saw book name is wrongly typed, you update the book name.

At last, if any book is out dated, than you have to delete that book. so you delete the book.

In this django tutorial, I will be making a django crud application.

Django CRUD Application :

I am not making class based view but function based view. So everything in this article will be function based view.

First things first.

Create a project, you can name it whatever you like. I name it sms. School Management System

django-admin startproject sms 

Now lets create an app named student. Because I am creating database for student. All the details of student will be there.

 python startapp student 

Lets start with Create View.

Django CRUD App : Create View and Read View

Go to in your student app and add these following lines.

from django.db import models
from django.urls import reverse

# Create your models here.
class Student(models.Model):
    student_name = models.CharField(max_length=100)
    father_name = models.CharField(max_length=100)

    date_of_admission = models.DateTimeField(auto_now_add=True)

    def __str__(self):

        return self.student_name 

    def get_absolute_url(self):

        return reverse('student-detail', kwargs={'id'})

Now migrate.

python makemigrations

python migrate


You must be thinking, what is the function get_absolute_url(self): its the function to go to the  detail of student and ‘student-detail’ is the name of the url path in

Go to of your app folder. and add these following lines :

# Register your models here.
from .models import Student


Now add following lines to your projects we have to configure file. it is very important.

sms/ ( This is main project’s

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('student/', include('student.urls')), # add this 

now create in your student app and add these following.

student/ ( This is app’s 


from django.urls import path
from . import views

urlpatterns = [
    path('', views.student_list, name='student-list'),
    path('<int:id>/', views.student_detail, name='student-detail'),
    path('create_student/', views.student_form, name='student-form'),
    path('update_student/<int:id>/', views.update_form, name='update-student'),
    path('delete_student/<int:id>/', views.delete_student, name='delete-student'),

Don’t worry about all the scary looking paths. You will understand everything once i finish this tutorial.


Now go to file in your app student. and add following codes.


from django.shortcuts import render, get_object_or_404, redirect

from .models import Student

def student_list(request):

    stu = Student.objects.all()
    context= {

    return render(request, 'student/student_list.html', context)


This is detail views  :


def student_detail(request, id):
    student_query = get_object_or_404(Student, id = id)
    context = {
        'student_query': student_query
    return render(request, 'student/student_detail.html', context)


Now add templates folder in your student app. After creating templates folder, now create student folder inside templates folder. Now create student_list.html and student_detail.html

Go to student_list.html and add these following lines inside <body> </body> tag  :

{% for s in stu %}
<table style=”border: 1px solid black; “>
<td><b>Father Name </b></td>
<a href=”{{ s.get_absolute_url }}”>{{ s.student_name }} </a>
      {{ s.father_name }}
<td><a href=”{% url ‘update-student’ %}”>Update </a></td>
<td><a href=”{% url ‘delete-student’ %}”>Delete </a></td>
{% endfor %}
Read View ( List View ) : 
list view

Above code will display all the students available in the database.

Now go to student_detail.html and add these following lines. These lines will be under <body> </body>

<p>{{ student_query.student_name }}</p>
<p>{{ student_query.father_name }}</p>
Now you are thinking, from where student_query came from, you must go to, where you have created a function named student_detail.
Inside that function you have created student_query. 
Detail View : 
detail view

Create View :

We have to make form to make create view, just like we create in admin panel, but we can’t give access of admin panel to everyone. we must need to make another page, where other user can enter information to database and view from there, update from there and delete from there.

create in your app’s folder.  student/

add these following lines.

from django import forms

from .models import Student

class StudentForm(forms.ModelForm):

    class Meta:
        model = Student
        fields = '__all__'

Now go to and add these following lines :

from .forms import StudentForm # you must import this from
def student_form(request):
    form = StudentForm(request.POST)
    if form.is_valid():
        return redirect('/student/')

    context = {
        'form' : form
    return render(request, 'student/create_student.html', context)

Now you must create a html file inside your app. Go to templates, than go to student, create a html file name it create_student.html 


Inside <body> </body>  add these following lines.

<form method=”POST”>
{% csrf_token %}
{{ form }}
<button type=”submit” name=”submit”>Add Student </button>
Create view : form 
crud application create view

We have made , create view, read view (also detail view). Now we have to make update view.
Go to, add these following lines.

def update_form(request, id):
    student = Student.objects.get(id=id)
    form = StudentForm(instance=student)
    if request.method == 'POST':
        form = StudentForm(request.POST, instance=student)
        return redirect('/student/')
    context ={

    return render(request, 'student/create_student.html', context)
update form : pre filled. 
crud application update view
To delete student, you must add these following lines in your file.

def delete_student(request, id):
    student = Student.objects.get(id=id)
    if request.method == 'POST':
        return redirect('/student/')

    context = {
    return render(request, 'student/student_delete.html', context)
Now create student_delete.html, where you have created all the html files.
add these lines inside <body></body> tag.

<h3>Are you sure you want to delete {{del_student.student_name}}</h3>
<form method=”POST”>
{% csrf_token %}
<input type=”submit” name=”Confirm” class=”btn btn-danger”>





Conclusion :  Django CRUD Application

I have not done any styling in this article, you must do some styling using bootstrap and css. That’s up to you. I have showed you how to create django CRUP application. I hope you have learnt this and apply this to your project. this is just a demo app.

Also read :  Make Password generator with Django 3.0 

Also read : Python facts you must know about . 


Leave a Reply

Your email address will not be published. Required fields are marked *