How to perform CRUD operations in Django

In this tutorial, we will learn how to operate CRUD functionalities in Django. CRUD functionalities are Create, Read, Update and Delete. These are the main building blocks of any project. At some point you need to perform read operations from the database, you want to do an update or you wish to delete an item from the database. All these are possible thanks to CRUD operations.

In this tutorial, we are building on top of these lectures on How to set up a Python Django Application using Django 4.0 and How to create Django Templates. So make sure to check out first.

If you want to follow along make sure you have the project setup complete, the templates are working fine, the database is up and running and lastly have some data on the database.

Steps to follow

1. Create a Project

To create a project go to your terminal and create a folder name your project whatever you like, the create a virtual environment where all your installations will be staying. To create a virtual environment do the following.

$ mkdir crud_example
$ cd crud_example
#create a virtual environment 
$ python3 -m venv env #env is the name of the virual environment.

To make your work easier open the containing folder in your favorite text editor, for me I will be using Pycharm. Activate the virtual environment as created above.

$ source env/bin/activate

2. Install Django

To install Django in your project, use the following command.

$ pip install django

After Django has been installed make sure you do pip freeze > requirements.txt in order to store all your installed files. This requiremets.txt file is necessary so that someone else who will come back later and want to use your project will just install the requirements from one file.

$ pip freeze > requirements.txt

3. Create a Django project and app

The next step is to create a Django project and an app. To create a project using the following command.

$ django-admin startproject myproject .

Make sure to include the period in the end. It tells Django to create a project in the current directory.

After the project has been created, we need to create an app. To do so use the following command.

$ python startapp project

When the app has been created, go to the file and add your app to the installed apps

#installed apps 

Still inside the file create a database of your choice, be it Mysql, PostgreSQL, or db sqlite. For my case, I will be using PostgreSQL. You may want to leave db sqlite and use it for testing purposes, but you will need PostgreSQL or MySQL for production.

4. Create PostgreSQL database

To use PostgreSQL, we need to create it first in our system and then tell django to use it. To create a Postgresql do the following.

Open psql as Postgres user

sudo -u postgres psql

To create a database, we use the following command inside postgres.


Project is the name of the database.

After creating the database, create the user

CREATE USER projectuser WITH PASSWORD 'strpassword';

Then we need to grant privileges to the user created.


Lastly quit the Postgres with \q

We now need to go back to our project and inside look for databases and change from db sqlite to PostgreSQL

    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'project_db',
        'USER': 'projectuser',
        'PASSWORD': 'strpasswrd',
        'HOST': 'localhost',
        'PORT': '',

Before we can connect to PostgreSQL, we need to install psycopg2.

pip install psycopg2-binary

Add it to the requirements.txt file

Create superuser

To create a superuser in Django we use the following command. Make sure to follow the prompts in order to create a user, email, and password

python createsupeuser

Add some data into the tables to play with.

5. Views, models, and Urls

Let’s start with creating models. Models give us the data and how our database will be structured. For this, I will use a simple example.

from django.db import models
import uuid

class Project(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField(null=True, blank=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):
        return self.title

After this make sure you run makemigrations and migrate in order to populate the database.

$ python makemigrations
$ python migrate

Create and populate

We need to create a file inside our app because Django doesn’t create it for us.

Your project/ file should look like this

from django.urls import path
from . import views

urlpatterns = [

For the main myproject/ it should look like this

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

urlpatterns = [
    path('', include('project.urls'))# add this to serve the app urls 

Create Views

This is the engine of your project. To create views use the following

from django.shortcuts import render

def index(request):
    context = {   
    return render(request, 'project/index.html', context)

5. Django Templates

From this part, we need to create templates and from the beginning, I had cover templates. How to create Django Templates.

Create a templates folder and add all the HTML in it. Create an index.html, projectfile.html, and delete.html files.

6. CRUD functionalities

Read Functionality

We can start with Read functionalities. Let’s display what is in the database in our home.

Remember the view we had created earlier, we are going to work from there. To understand more make sure you play with Django ORM.

This will be our final for Read operations.

from django.shortcuts import render
from .models import Project

def index(request):
    projects = Project.objects.all()# fetch all the data from the database

    context = {
        'projects': projects
    return render(request, 'project/index.html', context)

Create functionality

To use create functionality, we need to have a form where we input our data. So now head over to our app and add file and populate it with the following:

from django.forms import ModelForm

from app.models import Project

class ProjectForm(ModelForm):
    class Meta:
        model = Project
        fields = '__all__'

Then go to file and add this also

def createProject(request):
    form = ProjectForm()

    if request.method == 'POST':
        form = ProjectForm(request.POST)
        if form.is_valid():
            return redirect('home')
    context = {
        'form': form
    return render(request, 'project/projectfile.html', context)

Create urls to serve this view like this:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='home'),
    path('create/', views.createProject, name='create-file'),

Create Projectfile.html and populate with this:

<form method="POST" action="">
    {% csrf_token %}

    {{ form.as_p }}

    <input type="submit" value="Submit">

Update functionality

We are still going to use the form we created earlier. What is different only is the addition of a primary key to help us know the section we are updating.

On file add the following

def updateProject(request, pk):
    project = Project.objects.get(id=pk)
    form = ProjectForm(instance=project)

    if request.method == 'POST':
        form = ProjectForm(request.POST, instance=project)
        if form.is_valid():
            return redirect('home')

    context = {
        'form': form
    return render(request, 'project/projectfile.html', context)

Here I am still using the same projectfile.html file because its the same form am working on.

On project/ file add this URL link.

path('update/<str:pk>/', views.updateProject, name='update-file'),

Inside index.html file we need to add the edit link so that we can redirect to where we need to edit.

This should be the final index.html file.



    {% for project in projects %} # looping inside the function in django using tags 

        <td>{{project.title }}</td>
        <td>{{project.description }}</td>
        <td><a href="{% url 'update-file' %}">Edit</a></td>
        <td><a href="{% url 'delete-file' %}">Delete</a></td>
    {% endfor %}

Delete functionality

Delete functionality is the last and easiest of the three above. What we need to have is the deletefile.html and delete project view.

Let’s start with the deletefile.html. Populate it with the following.


<form method="POST" action="">
    {% csrf_token %}
    <p>Are you sure you want to delete "{{object}}"?</p>
    <a href="{% url 'home' %}">Go back</a># using url tags 

    <input type="submit" value="Delete">

Our URL is this.

path('delete/<str:pk>/', views.deleteProject, name='delete-file')

Create a delete view like this

def deleteProject(request, pk):
    project = Project.objects.get(id=pk)
    if request.method == 'POST':
        return redirect('home')
    context = {
        'object': project

    return render(request, 'project/deletefile.html', context)


We have successfully implemented the CRUD operation in Django. In case of any difficulty refer to my Github repo for the full codebase.

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

Your email address will not be published.