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 manage.py startapp project

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

#installed apps 
...
'project.apps.ProjectConfig',

Still inside the settings.py 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
postgres=# 

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

CREATE DATABASE project_db;

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.

GRANT ALL PRIVILEGES ON DATABASE project_db TO projectuser;

Lastly quit the Postgres with \q

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

DATABASES = {
    '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 manage.py 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.

#models 
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 manage.py makemigrations
$ python manage.py migrate

Create urls.py and populate

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

Your project/urls.py file should look like this

from django.urls import path
from . import views

urlpatterns = [
]

For the main myproject/urls.py it should look like this

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

urlpatterns = [
    path('admin/', admin.site.urls),
    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 views.py 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 forms.py 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 views.py file and add this also

def createProject(request):
    form = ProjectForm()

    if request.method == 'POST':
        form = ProjectForm(request.POST)
        if form.is_valid():
            form.save()
            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:

{% csrf_token %} {{ form.as_p }}

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 views.py 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():
            form.save()
            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/urls.py file add this URL link.

path('update//', 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.

Projects

{% for project in projects %} # looping inside the function in django using tags {% endfor %}
Title Description Action Action
{{project.title }} {{project.description }} Edit Delete

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.

Delete

{% csrf_token %}

Are you sure you want to delete "{{object}}"?

Go back# using url tags

Our URL is this.

path('delete//', 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':
        project.delete()
        return redirect('home')
    context = {
        'object': project
    }

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

Conclusion

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

Leave a Reply

Your email address will not be published.